Speedometer

I’ve this code above, to show the distance from start point, but how do I make a speedometer?

var Player : Transform;

var Dead : boolean = false;







function Start (){

Player = GameObject.Find("Cplayer").transform;

}





function Update () {

 

 var dist : float = Vector3.Distance(Player.position, transform.position);

 Debug.Log(dist);

 

 if(dist <= 30){

  Dead = true;

  }

 if(dist >= 200  Dead==true)

  {

  Destroy (gameObject);

  }

 }

Have you tried storing the the contents of Time periodically, then doing something like “distance / (Time.time - startTime)”?

NOpe i have tryed but it wont work with my coding

:frowning:

rigidbody.velocity is movement per second in units. So if your moving at a magnitude of 10 units per second it is easy to then convert that to kph

So 10 * 3.6 is the speed that you would be traveling at…

using a conversion table I found online… and some simple math…

1 unitpersecond = 3.6 kph
1 unitpersecond = 2.2369 mph

so…
var kph = rigidbody.velocity.magnitude * 3.6;
var mph = rigidbody.velocity.magnitude * 2.2369;

but how do i get that to a GUI?

You first need to start by learning the gui…

http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

Okay, i have start to try to see the kph, in delog with the code

var	addForceZ : float = 200.0;
var addForceX : float = 0.0;
static var rotateSpeed : float = 3.0;
var stopUr : float = 0.0;
var kph = rigidbody.velocity.magnitude * 3.6;

function Update () 
{

Debug.Log(kph);

It is not nesseriary to take the whole code.
but i get this error

ArgumentException: You are not allowed to call get_rigidbody when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
MoveAroundForce…ctor () (at Assets/Scripts/MoveAroundForce.js:5)

Then I tryed with start

var	addForceZ : float = 200.0;
var addForceX : float = 0.0;
static var rotateSpeed : float = 3.0;
var stopUr : float = 0.0;
var kph : int;
function Start(){
kph = rigidbody.velocity.magnitude * 3.6;
}
function Update () 
{

Debug.Log(kph);

but same error,

why`?

up…

var kph = 0;

function Update ()
{
kph = rigidbody.velocity.magnitude * 3.6;
Debug.Log(kph);

//your other code
}

OK, first, store KPH as a variable, and set it once per frame. You want to do this in the late update, not the update.
Next, put it on the screen using OnGUI… again, you need to look at the info not just write code then complain when it doesnt work.

var kph : int= 0;
var mph : int=0;

function LateUpdate (){
	kph = rigidbody.velocity.magnitude * 3.6;
	mph = rigidbody.velocity.magnitude * 2.2369; 
}

function OnGUI () {
	GUI.Label (Rect (10, 10, 100, 20), "Speed (KPH): " + kph);
	GUI.Label (Rect (10, 30, 100, 20), "Speed (MPH): " + mph);
}