gameobject.transform.position not working correctly

hey guys, so i try to move my object to certain coordinat, heres the code

if (gameObject.transform.position.x <= -49.7f )
        {
            gameObject.transform.position = new Vector3(49.08f, 1f, 0.0f);
        }

while the object correctly move to the X coordinat i wanted, but not the Y coordinat, it always move to 0.404 on Y coordinat, it drives me mad, i already try everything but no avail, hope anyone can help

edit :

heres the complete script

void Update ()
    {
        this.gameObject.transform.Translate(Vector3.left * platspeed * Time.deltaTime);

        if (gameObject.transform.position.x <= -49.7f )
        {
            gameObject.transform.position = new Vector3(49.08f, 1f, 0.0f);
        }
  }

> gameobject.transform.position not working correctly No, YOU do something wrong. Stop blaming Unity. Provide your whole script please. You surely move the object somewhere else

Try transform.x -= platspeed * Time.deltaTime; instead of this.gameObject.transform.Translate(Vector3.left * platspeed * Time.deltaTime); and tell me if it works.

As the documentation says : > If relativeTo is left out or set to Space.Self the movement is applied relative to the transform's local axes. Are you sure the rotation of your object is (0,0,0) ? If not, try transform.Translate(Vector3.left * platspeed * Time.deltaTime, Space.World);

I am having the same issue with a similar piece of code. In fact, if I do the following: gameObject.transform.position = new Vector3 (0.0f, 0.0f, 0.0f); The object moves!! (even though the object started at (0,0,0). Can anyone help?

Have you tried using void LateUpdate() instead of void Update() ? Other scripts may have been setting their own values for the transform(i.e. Character controller, NavMeshAgent).

3 Answers

3

Is it the child of another Game Object? Could try gameObject.transform.localPosition

This was the issue for me. You are a life saver.

Fantastic! Fixed my issue!

I am having the same issue with a similar piece of code.

In fact, if I do the following:

gameObject.transform.position = new Vector3 (0.0f, 0.0f, 0.0f);

The object moves!! (even though the object started at (0,0,0).

Can anyone help?

For anyone having this issue try using “transform.TransformPoint(Vector3.zero)” instead of using “transform.position”. It returns the object’s position in world space(as"transform.position" should do). This saved me after days of trial and error.