- Home /
How to turn on text in trigger zone
ive got some hovering text on my NPCs but i want to make them only show when the player is near them, so ive made a sphere collider (isTrigger) in an empty gameobject that is a child of the player and ive written a script turn on/off the GUIText component of the NPC depending on whether it is inside the sphere or outside but it doesnt seem to be working at all any help?
#pragma strict
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
var HealthText : GUIText;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
HealthText = GetComponent(GUIText);
}
function OnTriggerEnter (textenter : Collider)
{
if(textenter.gameObject.tag == "guitextcollider")
{
HealthText.enabled = true;
}
}
function OnTriggerStay (textenter : Collider)
{
if(textenter.gameObject.tag == "guitextcollider")
{
HealthText.enabled = true;
}
}
function OnTriggerExit (textexit : Collider)
{
if(textexit.gameObject.tag == "guitextcollider")
{
HealthText.enabled = false;
}
}
function Update () {
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
@script RequireComponent(GUIText)
Answer by NightShroud · Apr 16, 2014 at 02:55 PM
i figured out the problem, the gameobject that had the GUIText was moving everywhere while in Game Mode, so even though the Enemy was close, the text gameobject was far (or was close at the wrong time etc) fixed it by putting the script on another part of the enemy and breaking the script in two
Answer by ownerfate · Apr 15, 2014 at 11:16 PM
i would say, remove the " gameObject " from the if(textenter.gameObject.tag == "guitextcollider") in all the lines that uses that.
then try
if it doesn't work, i bet i know why it wont. but i just wanna make sure.
just want to make sure this is right
you have a sphere on your player? marked as trigger?
and this script is on the enemy? ?
the sphere is tagged " guitextcollider "?
yep, but the script is on a gameobject that is a child of the enemy, and that gameobject has GUIText on it
from what it looks like, i have tested out the script and the object just ends up waay off screen. ( GuiText also. )
tell me what you wanted the script to do, and i'll rewrite it. and explain through notes in it.
ah, ok i'm sorry that i may not have been a great help here... but i may know what is wrong and why your sphere is not working right with the object that you want.
i messed around with this for about an hour. and i found this interesting to me.
so far i can get the GUI Text to turn on and off when walking near and away from it,
and i made a little altercation to the script so you don't need a sphere.
but the problem seemed to be in the Else statement in the Start function.
here is your script with my changes. see if that works.
#pragma strict
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var use$$anonymous$$ainCamera = true; // Use the camera tagged $$anonymous$$ainCamera
var cameraToUse : Camera; // Only use this if use$$anonymous$$ainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
var HealthText : GUIText;
var PlayerTarget : Transform; // this is the player.
var Distance : int; // this can be altered to your liking on whether the detection is long or short.
function Start () {
Adder();
StartFunction();
}
function Adder (){
HealthText = GetComponent(GUIText);
}
function StartFunction(){
thisTransform = transform;
if (use$$anonymous$$ainCamera)
cam = Camera.main;
cam = cameraToUse;
camTransform = cam.transform;
}
function F1 ()
{
if ( Vector3.Distance(PlayerTarget.position, transform.position ) < Distance)
{
HealthText.enabled = true;
}
if ( Vector3.Distance(PlayerTarget.position, transform.position ) > Distance)
{
HealthText.enabled = false;
}
}
function Update () {
F1();
F2();
}
function F2(){
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = $$anonymous$$athf.$$anonymous$$ax(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3($$anonymous$$athf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
$$anonymous$$athf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
@script RequireComponent(GUIText)