Hi, I have this script: Code (CSharp): using System; using UnityEngine; public class Speedometer : MonoBehaviour{ public Rigidbody Rgbd; void Start(){ Rgbd = GetComponent<Rigidbody>(); } void FixedUpdate() { print(Rgbd.velocity); } } You are supposed to write on the console the speed in meters per second, or rather units per second. But no, write me this: (1.2, 0.0, 41.7) I'm getting angry because no matter how hard I try to make it work, the Unity documentation is useless. How is something as simple as a speedometer so difficult to program?
ISN'T! Sometimes throw random number, negative numbers, ugh. Thanks. Apparently, making a speedometer is a stupid endless mistery. It's harder than finding a needle in a haystack....
Velocity encodes direction and speed. The value you are getting is the units-per-second separated in the X, Y and Z axis. If you want a single number to use for a speedometer, use the magnitude of that vector: Code (CSharp): print(Rgbd.velocity.magnitude);
Regardless, the problem isn't anything to do with Unity. It's the fact you don't understand vectors. I'm only mentioning this because it's likely it will keep happening til you realise these are general problems that aren't anything to do with the engine (velocity is a Vector3 after all, and those have been around in C# and other languages forever - it's just 3 floats).
speed is basically a "simple" math function. speed = distance / time. where things can get tricky is that there are numerous units of measure, and you have to account for them when making something like a speedometer. for instance, unity units are in meters, and the deltaTime per frame is in milliseconds. you have to convert those values into the units you need in order to make a speedometer. Code (CSharp): public class MilesPerHour : MonoBehaviour { public float milesPerHour = 0f; [Space(20)] public float metersTravelledPerSecond; public float metersTravelledPerMinute; public float metersTravelledPerHour; // Got this value from a google search. Unity uses meters, // and one meter = 0.000621371f miles. private float metersToMiles = 0.000621371f; private Vector3 prevPosition = Vector3.zero; // Use this for initialization void Start () { prevPosition = transform.position; } // Update is called once per frame void Update () { Vector3 v = transform.position - prevPosition; float distance = v.magnitude; if (distance < 0.0001f) distance = 0f; // Get the meters travelled per second. metersTravelledPerSecond = distance / Time.deltaTime; // Meters per minute. There are 60 seconds in one minute! metersTravelledPerMinute = metersTravelledPerSecond * 60f; // Meters per hour. There are 60 minutes in one hour! metersTravelledPerHour = metersTravelledPerMinute * 60f; // Finally, multiply are speed - which is in meters - by our conversion variable to get the miles per hour. milesPerHour = metersTravelledPerHour * metersToMiles; prevPosition = transform.position; } }
Ok, finally worked, but the numbers are in float. I need in integer. I tried this: Code (CSharp): RigidVel = Rgbd.velocity.magnitude * 3.6 //converts meters per hour to kilometers Debug.Log(RigidVel.ToString("0")); But what happen? Unity is now dumb as me and says: "15,8: Unexpected symbol "Debug"" wtf
oh, idiot am I. I am using vs code, because, uh, the genius developers of unity removed monodevelop, and now we have that trash of vs, which is very slow.