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
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.
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?
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);
}
@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.