The weirdest thing keeps happening and I can not figure out why. Instead of just setting up like 20 different prefabs, I wanted to instatiate them in my code. I am making a Space Invaders C# game and I got the first row of invaders to spawn fine using the code found below. Literally no issues at all, and they move how I want them too as well. But as soon as I set up my second row the transform.position syntax that I used for the first row, suddenly does not apply and it just sets my transform.position.x to 0. I do have another class that MIGHT be the reason, but so far I have not found any solution to this problem. Thanks for help in advance.
//GameController Class
//
//globals
//
Quaternion roto;
public Vector3 newPos;
public float addInvader
bool spawnInvader = true;
public GameObject invaders;
//
void Update()
{
newPos = transform.position;
roto = transform.rotation;
newPos2 = transform.position;
roto2 = transform.rotation;
if (addInvader <= 0f)
{
SpawnInvaders ();
}
if (addInvader > 0f)
{
SpawnInvaders ();
}
if (addInvader > 5f)
{
spawnInvader = false;
}
if (add2Invader <= 0f)
{
//invaderSrow();
}
if (add2Invader > 0f)
{
//invaderSrow();
secondrow = false;
}
}
void SpawnInvaders()
{
if (spawnInvader)
{
if (addInvader < 1f)
{
newPos.x = 900f;
newPos.y = 0f;
newPos.z = 800f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
if (addInvader == 1f)
{
newPos.x = 700f;
newPos.y = 0f;
newPos.z = 800f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
if (addInvader == 2f)
{
newPos.x = 500f;
newPos.y = 0f;
newPos.z = 800f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
if (addInvader == 3f)
{
newPos.x = 300f;
newPos.y = 0f;
newPos.z = 800f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
if (addInvader == 4f)
{
newPos.x = 100f;
newPos.y = 0f;
newPos.z = 800f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
if (addInvader == 5f)
{
newPos.x = -600f;
//newPos2.x = newPos2.x - 200f;
newPos.y = 0f;
newPos.z = 500f;
roto.z = 180f;
Instantiate (invaders, newPos, roto);
addInvader++;
}
}
return;
}
void invaderSrow()
{
InvaderController F = new InvaderController();
//F.newX = newPos2.x;
if (secondrow)
{
if (add2Invader < 1f)
{
newPos.x = -800f;
newPos.y = 0f;
newPos.z = 500f;
roto.z = 180f;
//Instantiate (invaders2, newPos, roto);
add2Invader++; //add2Invader should be 1 now....
//secondrow = false;
}
if (add2Invader == 1f)
{
newPos2 = transform.position;
newPos2.x = -800f + 200f;
//F.newX = -800f;
//F.newX = newPos2.x + 200f;
newPos2.y = 0f;
newPos2.z = 500f;
roto2.z = 180f;
//Instantiate (invaders3, newPos2, roto2);
add2Invader++;
}
}
return;
}
//InvaderController class
//
//globals
//
public float newX;
public float invaderSpeed;
public float resistance;
public GameObject Invader;
public GameObject explosion;
Quaternion rotation;
//
void Awake()
{
firstxoff = true;
}
// Update is called once per frame
void Update ()
{
if (firstxoff == true)
{
firstmove ();
}
moveX1 ();
}
void firstmove()
{
Vector3 newPos = transform.position;
newPos.x -= invaderSpeed;
transform.position = newPos;
if (newPos.x < -900f) //moves z
{
Vector3 newPosZ = transform.position;
newPosZ.z -= invaderSpeed;
float x = -900f;
newX = x;
newPosZ.x = newX;
transform.position = newPosZ;
}
}
void moveX1()
{
Vector3 newPos = transform.position;
if (newPos.z <= 500f)
{
Vector3 newPosX = transform.position;
newX = newX + invaderSpeed;
float z = 500f;
newZ = z;
newPosX.z = newZ;
newPosX.x = newX;
transform.position = newPosX;
if (newPosX.x > -900f)
{
newX = newX + (invaderSpeed + 7f);
firstxoff = false;
}
}
}
in moveX1() you are using newX , is it possible you are sometimes calling moveX1 before firstMove ? if so then newX would be undefined and 0.
Figured it out. Added a newX = newPosX.x;
line at the start of my moveX1() function :D thanks everyone who tried!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.