Hello.
I am making an adventure game But i’m bad in coding so i don’t know how to do something like this:
What i want to do is create an GUI text when the
player approche a Gameobject and disaspear when the
player walks away.
Is it possible?
Hello.
I am making an adventure game But i’m bad in coding so i don’t know how to do something like this:
What i want to do is create an GUI text when the
player approche a Gameobject and disaspear when the
player walks away.
Is it possible?
http://docs.unity3d.com/Documentation/ScriptReference/GUIText-text.html
Even if you’re bad at coding, it’s a good idea to read the manuals first.
First answer: Yes. Definitely possible. Dave’s suggestion of using a trigger is definitely a good idea, though you could also set a distance. Here’s a snip of code from a label that fades in and out based on the distance you are from it, and rotates to face the camera. Read through it a bit and then rework it for what you need if you’d like:
//Set the distance at which the object fades, and how far it takes.
var FadeStart : int = 150;
var FadeDist : int = 5;
//Which items should fade?
var Panel : GameObject;
var TextBox : GameObject;
private var FadeEnd : int;
function Update () {
//Check the distance between the active camera and the text
var dist = Vector3.Distance(Camera.main.transform.position, transform.position);
FadeEnd = FadeStart+FadeDist;
//Rotate the text to face the camera, as long as you're more than 5m away
if(Panel.renderer.isVisible){
if(dist>5){
transform.LookAt(Camera.main.transform);
transform.eulerAngles.x = 0;
}
//Fade the text and box, with an alpha between 0 and .75
if(dist>FadeStart && dist<FadeEnd){
var renderAlpha = 1-(dist-FadeStart+(.4*FadeDist))/FadeDist;
Panel.renderer.material.color.a = renderAlpha;
TextBox.renderer.material.color.a = renderAlpha;
Debug.Log(renderAlpha);
}
if(dist>=FadeEnd){
Panel.renderer.material.color.a = 0;
TextBox.renderer.material.color.a = 0;
}
if(dist<=FadeStart){
Panel.renderer.material.color.a = 0.75;
TextBox.renderer.material.color.a = 0.75;
}
}
}