Calculate the distance a object has moved.

Hi,

I'm trying to calculate the distance a object has moved to be able do decrease fuel (in a vehicle).

Or is best to calculate de overall time the object has been moving??

thanks

float distanceTravelled = 0;
Vector3 lastPosition;

void Start()
{
  lastPosition = transform.position;
}

void Update()
{
  distanceTravelled += Vector3.Distance(transform.position, lastPosition);
  lastPosition = transform.position;
}

Here is how i converted the entire code to javascript

`private var distanceTravelled : float= 0;
private var lastPosition:Vector3;
function Start()
{
lastPosition = transform.position;
}

function Update()
{
distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
}`

Script Conversion to Javascript :

var DistanceTravelled : float = 0;
var lastPosition : Vector3;

function Start()
{
//cached data.
lastPosition = transform.position;
}

function Update()
{
DistanceTravelled += Vector3.Distance(transform.position, lastPosition);

lastPosition = transform.position;
}