- Home /
Adjusting localPosition
Hi, I am trying to keep an object within its parents boundaries. So I created this script:
Vector3 Position = transform.localPosition;
if (Position.x > 0.5f)
{Position.x = 0.5f;}
print (Position.x);
In order to prevent the object from moving over the 0.5f treshold. However, the object simply doesn't respond to this treshold.
What am I doing wrong?
@Brokenarrow Well, there are more things to worry about than just the x position. What about the y and z? Also, make sure this function running in update
Im aware of the Y and Z, this is just as an example. It is running in Update.
It simply ignores the 0.5f treshold. The object can cross the boundary freely.
(print (Position.x); does stop at 0.5f however)
Answer by CanisLupus · Jun 24, 2014 at 10:19 PM
You're not setting the position of the object with Position.x = 0.5f
. That instruction only sets the x value of the Vector3 named "Position". To set the object's position you need to do transform.localPosition = Position
after your example. Does this make sense?
@Brokenarrow oh yeah. @CanisLupus makes a valid point. You're copying the vector and modifying it. That won't affect the Transform.
Thank you very much, I can't believe I didn't see that!
Your answer
Follow this Question
Related Questions
Find a gameobject with the same position 2 Answers
Distribute terrain in zones 3 Answers
Updating localPosition(?) in a scaled transform parent 1 Answer
Set child position and move parents with 2 Answers
Scale gameobject around vector 0 Answers