Hi,
I want to move objects in KM/H,for example 100KM/H.how can i do that?
Thanks.
Hi,
I want to move objects in KM/H,for example 100KM/H.how can i do that?
Thanks.
Just divide 100 km/h by 3.6. Like this:
function Update()
{
transform.Translate(Vector3.forward * 100 / 3.6 * Time.deltaTime);
}
and that should do the trick.
Units in Unity are generic, they are not of a specific type as meters, miles, grams or seconds. Depending on what you want to do, you might want to map them to different real-world units.
That said, it’s good to stick to how those unity are mapped commonly, also because the physics engine acts differently depending on the scale of objects.
Commonly the units are defined as follows:
Distance: meters
Time: seconds
Mass: kilograms
(If you want to do a space simulation, you’ll probably want to use a different mapping).
So, if you want to move an object with 100 km/h, you need to convert that to 27.8 m/s (google “100 km/h in m/s”). Then you use Time.deltaTime to apply the velocity to the object each frame:
function Update() {
// move the object forward, however it is rotated
transform.Translate(Vector3.forward * 27.8 * Time.deltaTime);
// or move the object in world-space
transform.position += new Vector3(1, 0, 0) * 27.8 * Time.deltaTime;
}
i.e. since each frame can take a different amount of time, you need to consult Time.deltaTime to figure out how much time has elapsed since the last frame. Then you multiply the speed (m/s) with the elapsed time (s) to get the distance the object traveled (m) and update the object’s position accordingly.