- Home /
How can I reset the y position of a gameobject to a certain value each frame?
In void start I have "y = transform.position.y;" and in void Update (at the very end) I have "y = 16;" because that's what I want the y value to be at all times.
My theory is that this will lock down the y without changing any of the other coordinates so that even though a bug in my code is somehow changing it, this should overwrite it and work as a temporary fix, but it's not doing anything. My gameobject still moves on the y axis. Any ideas? Thanks!
Comment
Answer by UnityCoach · Jan 04, 2017 at 09:44 AM
When you do
float y; // this variable will store a float value
void Start ()
{
y = transform.position.y; // y is now the same as the position of the transform on y
}
void Update ()
{
y = 16; // y is now 16
}
What you want to do :
Vector3 position; // this variable will store a Vector3 value
void Update ()
{
position = transform.position; // get the position
position.y = 16; // assign 16 to its y component
tranform.position = position; // assign the position back to the transform
// you can't assign individual components of a Vector3, because Vector3 is a Struct, you have to do it like the above, not like this
//tranform.position.y = 16;
}