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 bla$$anonymous$$g Unity.
Provide your whole script please. You surely move the object somewhere else
i add the full script with the update function
Try
transform.x -= platspeed * Time.deltaTime;
ins$$anonymous$$d of
this.gameObject.transform.Translate(Vector3.left * platspeed * Time.deltaTime);
and tell me if it works.
transform.x -= platspeed * Time.deltaTime;
Transform has not any property / attribute called
x
If you meant
transform.position.x
, you can't directly modify one component of the direction vector
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);
still not avail
the problem is not on the translate, but on this code :
gameObject.transform.position = new Vector3(49.08f, 1f, 0.0f);
everytime it execute that script, the Y always on 1.404 not fix 1, but the X work correctly
Have you tried to disable this script and see if another script moves your object ?
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() ins$$anonymous$$d of void Update() ? Other scripts may have been setting their own values for the transform(i.e. Character controller, Nav$$anonymous$$eshAgent).
Answer by brett_henning · Aug 17, 2017 at 08:32 AM
Is it the child of another Game Object? Could try gameObject.transform.localPosition
Answer by zedsmith52 · May 10, 2017 at 04:55 PM
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?
Answer by spospider · Aug 21, 2019 at 02:15 PM
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.