最奇怪的事情不断发生,我无法弄清楚原因。我想在我的代码中实现它们,而不仅仅是设置20个不同的预制件。我正在制作一个太空入侵者C#游戏,我使用下面的代码得到第一排入侵者。字面上根本没有任何问题,他们也移动我想要的方式。但是一旦我设置了第二行,我用于第一行的transform.position语法突然不适用,它只是将我的transform.position.x设置为0.我确实有另一个类可能是原因,但到目前为止,我还没有找到解决这个问题的方法。提前感谢您的帮助。
//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;
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
想出来。我在moveX1()函数的开头添加了newX = newPosX.x;
行:感谢所有尝试过的人!