- Home /
Move character to abolute z position
I need to move a character to an exact z position but the same x and y
I tried this
newZPos = 1.5f;
transform.position = new Vector3(transform.position.x, transform.position.y, newZPos)
but all it does is the character falls through the ground and keeps dropping (i.e. the Y changes)
Any ideas??
Answer by Ghopper21 · Nov 10, 2012 at 11:46 AM
Sounds like there's nothing to prevent gravity from taking over in your new Z position. If you don't want that to happen, you could uncheck the "Use Gravity" option in the object's Rigidbody component. Or you could make sure there's some kind of floor under the object that prevents it from falling.
Where I am moving it to is only a fraction away. It should be a bit of a nudge really. I have just run it with the character selected and with a debug.log in the code, and in the Inspector it is saying that the Y is 0.08 and the debug.log is going crazy with the Y alternating between around -4.2 and -8.9 even though the character is still. This would explain my problem, it is moving my character to a position under the ground. But why the different Y positions in the Inspector and code.