简体   繁体   中英

Unity C# bool keeps turning true

Very confused about what's going on. I will post code, but just a brief description of my game is a space invaders remix. I have a CPU that is currently moving on the X axis, stops when it hits -900f, then moves down on the Z axis. Now I am trying to get to to stop moving down on the Z axis (accomplished), then move in the opposite X direction is was previously going in (before the X axis was moving 7f every update, I want it to move -7f every update from that point forward). My problem is that now when it hits the designated Z axis to stop, then move on the X axis, it starts to move, but then keeps going back to -900 on the X axis. In my code below you will see that I have the program telling the transform.position.x to equal -900f, however, I put it inside an if loop that only run that code if a bool value is true, which I set to false after I want the program to ignore the code that shows the x position to -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;
}

First time z is less than 500, your x will increase 7, right?

newX = newX + invaderSpeed;

So you go from -900 to -893. -893 is not greater than 800, so firstxoff is not set to false. The firstmove method is then called again.

newPos.x -= invaderSpeed;

X is now -893 minus 7, which is -900. That cycle will repeat perpetually. Z will always be 500 and x will always be -900.

Hopefully you can figure out the solution. Let me know if not.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM