- Home /
Is there a way to set velocity while setting the position?
I noticed when I try to set the position of a rigidbody, it doesn't change velocity.
Is there any easy way to set the position and having the game engine automatically recognize what the velocity is supposed to be?
Answer by adrenak · Jan 19, 2012 at 06:17 PM
Unity can't assume with what velocity you want to go because it depends on how much time you want the object to take for it to go from point A to B. But theres a really simple method of moving a object to a point with a desired velocity :
var destintationPoint : Transform; //place you wanna go
var speed : float; //speed with which you wanna go
function Update () {
transform.position = Vector3.Lerp(transform.position, destinationPoint.position, Time.time * speed);
//or you can use substitute Lerp with MoveTowards for a more uniform velocity
}
I Hope this helps.
But see, when you are moving an object with transform.position=, even smoothly, the actual velocity of the rigidbody stays at zero because you are only changing the position.
So, imagine a car traveling 50mph and for one script loop, you set the transform.position to (0,0,0). The car is going to keep going 50mph from that point.
In a different game engine I used before, there was an auto velocity feature where in that same case, it would detect the change and stop the car completely.
This is just an example though.. The problem I'm currently dealing with is a little more complex.
Your answer
Follow this Question
Related Questions
How can I increase or decrease a position based on another variable ? 1 Answer
Problem stopping rigid body after adding a force 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Get Interpolated rigidbody velocity 1 Answer
Take final position after a force applied to a GameObject 1 Answer