# if else simple question, object position

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));

}
}
``````

}

Thanks guys!

if you want the object to move until it gets to around -2000 in the x axis then change position to -6000,0,0 then the following will do it.

``````using UnityEngine;

public class ObjMover : MonoBehaviour
{
private bool _stopMoving;

public void Update()
{
if (!_stopMoving)
{
if (transform.position.x <= -2000.0f)
{
transform.Translate(new Vector3(6000, 0, 0));
_stopMoving = true;
}
else
{
transform.Translate(new Vector3(-5, 0, 0));
}
}
}
}
``````

if you don’t want it to just stop when you reposition it (which is the same as 4000,0,0 btw) then just remove the `_stopMoving` check.

in C# you need to use ‘`new`’ when creating new `Vector3`’s and checking components of positions can be done per axis.

i’m not sure if that’s what you really want, or why, but it’ll do what you seem to be wanting, but ymmv…

Hi there!

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:

``````if (transform.position.x <= -2000) {
transform.position.x += 6000;
}else{
transform.Translate(new Vector3(-5, 0, 0));
}
``````

Scribe

Replace

``````if (transform.position == Vector3(-2000,0,0)) {

transform.Translate(Vector3(6000,0,0));
}
``````

With

``````if(transform.position.x <= -2000)
{
transform.position = new Vector3(6000,
transform.position.y,
transform.position.z);
}
``````

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.