How to make an object move constantly? / Cómo hacer para que un objeto se mueva constantemente?

Hello. I want the object that this script has to move to the right constantly since it appears if its position is -60 (a prefab is created).
And I scripted this:

void Update()
{
if (gameObject.transform.position == new Vector3(-60,0,0))
{
gameObject.transform.Translate(200f * Time.deltaTime, 0, 0);
}
}

But there is a problem, it does not move all the time, it appears and moves only once. I don’t know why this happens because it is in a void update.

(sorry for the bad english)

You’re currently checking if the position is exactly (x: -60, y: 0, z: 0) before moving.
Once the object moves, the position is no longer exactly that value, so the condition evaluates to false (not to mention the whole floating-point precision problem also).

If you just want the object to move constantly, is the condition even needed? Why not just omit it?

void Update() {
  transform.Translate(200f * Time.deltaTime, 0, 0);
}
2 Likes

The condition is important, because my idea is to have the prefab created at -60 or 60 randomly. If it is created at -60 it moves to the right, and if it is created at 60 it moves to the left.
Thanks for your attention

Then you either check the condition in Start() only, or simply give the script a starting velocity when you spawn it. There’s no need to check the position in update nor does doing so make any sense.

3 Likes

Thank you! Now that I think about it, it makes sense. I asked a stupid question, is that I am a newbie

I just wanted to ask if i want an an enemy to move left if it collides with another game object and move right if it collides with another gameobject but it only moves once