- Home /
When I use rigidbody.Moveposition, the object doesn't move accurately.
I've been using rigidbody.Moveposition for one of the objects in my game. It moves up for a set number of seconds, then moves back down for the same number of seconds, at the same speed. However, the object doesn't always move correctly. Sometimes it'll move higher than it should, but then move the right distance down, or vice versa. How can I make the movement more exact? This is the snippet of code I'm using to make it move:
if(spinDirection == "up")
{
rigidbody.MovePosition(rigidbody.position + Vector3(0,30,0) * Time.deltaTime);
}
else if(spinDirection == "down")
{
rigidbody.MovePosition(rigidbody.position + Vector3(0,-30,0) * Time.deltaTime);
}
Answer by jenci1990 · Dec 01, 2014 at 08:47 PM
Use in FixedUpdate() not in Update(), and change "Time.deltaTime" to "Time.fixedDeltaTime".
Thanks, that seems to have worked! What's so different about it though that it makes it work?
The FixedUpdate called every (default) 0.02 seconds, but the Update called every frame, it's independent of time. Read it: http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html