Evening,
As the subject of the topic says would like to ask if there is anyway of knowing the velocity of a car at a certain point,for example begining of the curve.
Thanks in advanced,
Hiroz
if the car has a rigid body attached, you can ask for the velocity of it ![]()
What if a gameobject isn’t a rigidbody? How would you get the velocity?
I dont know a straight wayt to get the velocity without using a rigidbody.
What you can do is creating measures of the car’s position in time, and then with some math, guess the velocity.
IMHO is easier to use rigidbody ![]()
You could store the GameObject’s position in a variable called something like lastPosition each update. Then, say a few seconds later, compare the current position with lastPosition to find the amount of space the GameObject has traveled. Divide that by the time it took to get there, and you have the speed. In other words:
Velocity = Distance / Time
It’s basic physics, and you can find loads if introductory material on the net. ![]()
Edit: Here’s a decent resource: The Coding Train. It’s mainly aimed at Processing (Java), but all the concepts explained are perfectly applicable within Unity. Look at the Vectors section for more about things like velocity.
You can get a continuous reading of a rigidbody’s velocity like this:
var car : Rigidbody;
function Update () {
carSpeed = car.rigidbody.velocity.magnitude;
Debug.Log(carSpeed);
}
Then use triggers to catch the speed at different points on your track.