Measuring distance walked in first person view

For a psychological experiment for my thesis I want to measure the distance someone walks in centimetres in a 3D virtual environment I will create in Unity. Is this possible? If yes, how? And is it also possible to measure the time in (milli)seconds?

If someone will try to answer my question I will be very grateful!

Use Vector3.Distance. Units in Unity are generally considered to be metres.

Really basic stuff that I’m sure a search would’ve yielded. For the sake of answering and closing this question (and for science!), I shall give you some code to demonstrate…

public var totalDistance : float;
public var totalTime : float;

private var lastPos : Vector3;
private var currentPos : Vector3;

function Start()
	totalDistance = 0;
	totalTime = 0;
	lastPos = transform.position;
	currentPos = transform.position;

function Update()
	currentPos = transform.position;
	totalDistance += Vector3.Distance(currentPos, lastPos);
	totalTime += Time.deltaTime;
	lastPos = currentPos;

function OnGUI()
	GUI.Box( Rect( 20, 20, 200, 25 ), "Distance = " + totalDistance.ToString() );
	GUI.Box( Rect( 20, 50, 200, 25 ), "Time = " + totalTime.ToString() );

Very possible! It’s a common thing to need to store something and check changes to it; this is how I usually do it. Have a “last value” and update that at the end of the Update function; at the beginning of Update you use it to find the delta value. Initialize it in Start so it doesn’t jump to 1000 or something by default.

public class MovementMeasurer : MonoBehaviour {
  public float GetDistanceTraveled() { return distanceTraveled; }
  public float GetAverageSpeed() { return distanceTraveled / timePassed; }

  float distanceTraveled = 0f;
  float timePassed = 0f;
  Transform tx;
  Vector3 lastPosition;

  void Start() {
    tx = transform; // cache because why not and tx is shorter to type
    lastPosition = tx.position;
  void Update() {
    distanceTraveled += (lastPosition - tx.position).magnitude;
    timePassed += Time.deltaTime;
    lastPosition = tx.position;