Display speed of an object

Hi,

I’m trying to write a Javascript that displays the speed of my object as a Gui.
But it’s proving to be quite difficult. I’ve looked for that old 1.x example project with the orange car that had a speedometer but I don’t think it’s on the site anymore :frowning:

var gui : GameObject;

function Update () {
		
var speed = ???????????????????????
gui.guiText.text = "Speed: " + speed.ToString(); 

}

Any ideas would make me very happy :slight_smile:

Thanks,
Stuart

if you want the speed to be located over the object moving, you can do this:

var myObject : GameObject;

function OnGUI()
{
  if (myObject) {
    var mySpot : Vector3 = Camera.main.WorldToScreenPoint(myObject.transform.position);
    GUI.Label(Rect(mySpot.x - 50, Screen.height - mySpot.y - 10, 100, 20), myObject.rigidbody.velocity.magnitude.ToString());
  }
}

If it’s just displaying it, then do the GUI.Label wherever you want it and use myObject.rigidbody.velocity.magnitude.ToString();.

Thanks GargerathSunman I used your advice and did this:

var myObject : GameObject;
var gui : GameObject;

function Update () {
		
gui.guiText.text = "Speed: " + myObject.rigidbody.velocity.magnitude.ToString() + "m/s"; 

}

Thanks,
Stuart