I do have a box collider(the teleporter, if sum1 goes through works fine).the problem is: i wonna show a GUI Text, if the fps controller is around 5 meters near to the box. And if hes through the teleporter(more then 5 meters away)the gui text should get invisible!
I hope you understand my problem //i was searching for this problem, but i didnt find any solution, or i guess i used the wrong keywords??
Celofa
Edit//found a solution:
var cube : Transform;
var gutext : GUIText;
function Update () {
var dist : float = Vector3.Distance(cube.position, transform.position);
if(dist<50){
gutext.text="Teleport to Burnard";
}
else
{
gutext.text="";
}
}
If the teleporter already has a collider, I would parent a gameobject to the teleporter with a sphere collider, set the collider to be a trigger, and set the radius of the sphere to be the size you want the area around the teleporter to trigger a GUI Label. (Eg: a radius of 5 would be a 5 meter radius around the teleporter).
Then, I would use OnTriggerStay() and OnTriggerExit() to determine when to show the GUI Label with a boolean, and while OnTriggerStay() is active, update the position of the teleporter to 2d screen coordinates every frame so you know where to put the label.
Something like this:
public var teleporter : Transform; //drag your teleporter object here, so we can find its 2d position
private var teleporter2DPos : Vector3;
private var showGUILabel : boolean = false;
function OnTriggerStay(other : collider){
if(other.CompareTag("Player")){
showGUILabel = true;
teleporter2DPos = Camera.main.WorldToScreenPoint(teleporter.position);
}
}
function OnTriggerExit(other : collider){
if(other.CompareTag("Player"))
showGUILabel = false;
}
function OnGUI(){
if(showGUILabel == true)
GUI.Label(Rect(teleporter2DPos.x, teleporter2DPos.y, 100, 20), "Label Text Goes Here");
}
to find the distance between 2 objects.
You can then make an if using this to display a GUI.Label.
If you insist on using a GUIText, you could change the enabled variable.
Example:
//BeginCode
var distance : float; //Define the variable at start
function Update(){ //This is executed every step
distance = Vector3.Distance(object1.transform.position,object2.transform.position); //Assign the distance between object1 and object2 to the predefined distance variable
if(distance<5){ //Check if the distance is less than 5
print("GUI Text wird angezeigt"); //Print the text
}
}
//End Code