【发布时间】:2013-12-31 01:38:44
【问题描述】:
对发生的事情感到非常困惑。我将发布代码,但我的游戏只是一个简短的描述是一个太空入侵者混音。我有一个 CPU 当前在 X 轴上移动,当它达到 -900f 时停止,然后在 Z 轴上向下移动。现在我试图停止在 Z 轴上向下移动(已完成),然后在相反的 X 方向上移动是先前进入的(在 X 轴每次更新移动 7f 之前,我希望它每次移动 -7f从那时起更新)。我的问题是,现在当它碰到指定的 Z 轴停止,然后在 X 轴上移动时,它开始移动,但随后在 X 轴上一直回到 -900。在下面的代码中,您将看到我有程序告诉 transform.position.x 等于 -900f,但是,我将它放在一个 if 循环中,如果 bool 值为 true(我将其设置为 false),则该循环仅运行该代码在我希望程序忽略将 x 位置显示为 -900f 的代码之后。
public class InvaderController : MonoBehaviour {
public float resistance;
public int resistcount;
public int numhits;
public float newX; //set to public so I can see it change in Unity, will go private
float newZ;
public float invaderSpeed; //this is set to 7 in Unity
public GameObject Invader;
Quaternion rotation;
public GameObject explosion;
int expcount;
bool firstxoff = false;
bool test = true;
public GameObject explosion2;
// Use this for initialization
void Start () {
}
void Awake() {
firstxoff = true;
}
// Update is called once per frame
void Update () {
if (firstxoff == true)
{
firstmove ();
}
}
void firstmove()
{
Vector3 newPos = transform.position;
newPos.x -= invaderSpeed;
//newX = newPos.x;
transform.position = newPos;
if (newPos.x < -900f)
{
Vector3 newPosZ = transform.position;
newPosZ.z -= invaderSpeed;
float x = -900f;
newX = x;
newPosZ.x = newX;
transform.position = newPosZ;
}
moveX1 ();
}
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 (newX > 800f)
{
firstxoff = false;
}
}
}
void FixedUpdate() //need to figure out how to run update first, then this.
{
//firstxoff = false;
}
【问题讨论】: