calculating distance an object moved

Enock and I have begun development of our game. I have even started filming our development. We are well into week 3. Well, one of the code snippets i was tasked to developed involed the calculation of our how far our object has moved throughout the game. If the doll has moved 30 meters- we need to keep track of it. Especially when she dies so that this can be tallied up on a piece of paper that will appear on death().

I found a script in C# that i thought did what I needed, but in unity- it throughout errors. I converted it–somehow into javascript. It is below: There are two private var set up. that shows the distance traveled and the last position. You can view the old code here  http://answers.unity3d.com/questions/16107/calculate-the-distance-a-object-has-moved.html   I went in and added my 2 cents sense another user was having the same issue. The last thing a user needs to do is  create a guiText field that will plug into their GUI, which then will feed the results.

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;
}

Comments are closed.