Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NightShroud · Apr 15, 2014 at 06:21 PM · text

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)
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NightShroud · Apr 16, 2014 at 02:39 AM 0
Share

still not working, what next?

avatar image ownerfate · Apr 16, 2014 at 08:54 AM 0
Share

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 "?

avatar image NightShroud · Apr 16, 2014 at 08:59 AM 0
Share

yep, but the script is on a gameobject that is a child of the enemy, and that gameobject has GUIText on it

avatar image ownerfate · Apr 16, 2014 at 09:11 AM 0
Share

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.

avatar image ownerfate · Apr 16, 2014 at 01:37 PM 1
Share

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)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUIText Help 1 Answer

Fall down a line in Unity 4.6 text via script 3 Answers

load line from a text file and separate it 4 Answers

What is wrong with my text changing script? 1 Answer

3D Text Object does not change to the color specified on Mouse Over 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges