- Home /
How to get distance between player and object
I need to know how to get the distance between the player (first person controller) and a object such as a car (its for an objective) and I need it to auto update with every step
var distance : GUIText
var other : Transform;
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
GUI.text (Rect (10, 10, 100, 20), ""Distance to other: " + dist");
}
Answer by robertbu · Jun 08, 2013 at 11:37 PM
GUI and GUIText are two very different things.
GUI functions must be executed inside OnGUI() {}.
Things you want executed every frame, can be put inside Update() {}.
pragma strict
var other : Transform; private var dist : float;
function Update() { if (other != null) { dist = Vector3.Distance(other.position, transform.position); } }
function OnGUI() { GUI.Label(Rect (10, 10, 200, 20), "Distance to other: " + dist); }
Oh okay, Thank you so much this script worked!! =) Really helped out!
Answer by aldonaletto · Jun 08, 2013 at 11:37 PM
This script has several errors: you're messing GUIText and the GUI system. If using the GUI system, the code could be like this:
var other: Transform;
function OnGUI(){
if (other){
var dist = Vector3.Distance(other.position, transform.position);
GUI.Label(Rect(10, 10, 100, 20), "Distance to other: " + dist);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
FPS Instantiate position. Please help? 0 Answers
How to Make a Better Environment? 1 Answer
How To Set Scope Speed 2 Answers
Muzzleflash help 0 Answers