Distance counter

Hello,
I’m trying to make a counter for my game that are showing how far the carachter have run from the starting position. My friend sent my this script but I can’t seem to make it work.

@script ExecuteInEditMode() //Will work in your game view, not scene view

var target : Transform; //What to follow

function Update() {
    if(target) transform.position = camera.WorldToViewportPoint(target.position);
}

I have attached the script on to a GUIText, and I set “Player” as target. But I get an error saying: There is no ‘Camera’ attached to the “GUI Text” game object.

Please help me, I have googled for hours without finding a solution :frowning:

You’re referencing a camera in the script, so of course if one isn’t attached to the GameObject then the code will error. If you’re just using the default camera then try Camera.main.

http://unity3d.com/support/documentation/ScriptReference/Camera-main.html

Thank you! That took care of the error… but the counter isn’t working. The text is just saying “GUI Text” and it’s following the player. Have I attached the script on to the wrong object?

var target : Transform; //What to follow

Have you dragged your player onto the target field of the camera you attached this script to?

what is the script actually trying to do?

I don’t think your script resembles a distance counter

The script does nothing useful at all.

This saying if the target exists then position the target at the camera view point. What has this got to do with distance at all?

The following code will output the total distance travelled every frame.

var origPos : Vector3;
var currentPos : Vector3;
var myTransform : Transform;

function Start()
{
    myTransform=transform;
    origPos=myTransform.position;
    currentPos=myTransform.position;
}

function Update()
{
    currentPos=myTransform.position;
    Debug.Log((currentPos-origPos).magnitude);
}

Thank you so much! But how exactly do I use the script? I’m pretty new to coding, it’s often my friend who handle that part of the game development.

I think I got it to work, but how do I make the stats show up in a GUI Text instead of the debug log?

Istead of this:

Debug.Log((currentPos-origPos).magnitude);

I tried this:

GUIText((currentPos-origPos).magnitude);

But I got an error saying: The type ‘UnityEngine.GUIText’ does not have a visible constructor that matches the argument list ‘(float)’.

Can somebody please help me …?

Bump!

GUIText is a component, .text is the actual text field

If you’re calculating how many meters the character has moved in total, why not just use something like:

DistanceMoved += MoveSpeed * Time.deltaTime;

When the character moves?

@script ExecuteInEditMode() //Will work in your game view, not scene view

var target : Transform; //What to follow

function Update() {
    if(target) 
    {
        transform.position = camera.WorldToViewportPoint(target.position);
        guiText.text = Vector3.Distance(target.position, transform.position);
    }
}

I think that should suffice, if you need me to explain i will.