First time asking a question, sorry its probably terribly obvious what I’m doing wrong here. I’m trying to have my object move in the ‘x’ axis until it hits -2000, at which point it would be sent to +6000 to loop infinitely. any help on where i’m going wrong?
void Start() { }
// Update is called once per frame
void Update() {
if (transform.position == Vector3(-2000,0,0)) {
transform.Translate(Vector3(6000,0,0));
}
else
{
// transform = the transform of the object this script is applied to.
transform.Translate(new Vector3(-5, 0, 0));
}
}
Because Update is not called continuously (in the mathematical sense, it is called at fixed points in time, not all the time), the likelyhood of the transform.position ever being exactly -2000 is very slim. If it is -2000.0000001 is will not return true etc. Therefore a better method would be to check if only the x position is less than or equal like so:
I only broke the “new Vector3” chunk into separate lines for readability, you can have it all on one line if you’d like.
To get the desired re-positioning effect. The same check could be done with >= 6000 to reassign the position to -2000 if you want to flow back the other way as well. Also, if you would like the movement to be consistent, you will likely want to move the translate out of the else block and just have it run in Update() normally.