RaycastHit to call GUI Text

so i’m using raycast hit to detect gameobject and then execute animations and load scenes, but the problem is when i want to add GUI text interaction with raycast hit, the physics.raycast detects all of the gameobjects so the gui text appears everywhere. how to make the raycast hit to be more specific in checking gameobject/ignore the others?

7008-1.jpg

here’s the script :

var distance : float =2;
var theGuiObject : GUIText;
var liftSound : AudioClip;
var Metal : GameObject; 
    
function Update()
{
var dir = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;

Debug.DrawRay(transform.position, dir * distance, Color.blue);

if (Physics.Raycast(transform.position, dir, hit, distance))
{ theGuiObject.enabled = true;
  if(Input.GetKey(KeyCode.E) && (hit.collider.gameObject.tag == "trigger")){
          audio.PlayOneShot(liftSound, 1.0 / audio.volume);
           Metal.animation.Play("LiftUp");
         
     }
} else { theGuiObject.enabled = false; }

if (Physics.Raycast(transform.position, dir, hit, distance))
{
  if(Input.GetKey(KeyCode.E) && (hit.collider.gameObject.tag == "turun")){
          audio.PlayOneShot(liftSound, 1.0 / audio.volume);
           Metal.animation.Play("LiftDown");
         
     }
} 

}


any suggestions?

I’m not completely sure what you are asking. I’m assuming you want to eliminate some object from the Physics.Raycast(). One way to to assign your object to layers. Then you can use the layerMask parameter to specify which layer you want to use for the Raycast.

A second way is to attach a script containing properties to your objects. They you can check the a property of the object to see if the GUI Text is to be displayed. For example if you had a script called “Prop” attached to many of the objects you could do something like:

    if (Physics.Raycast(transform.position, dir, hit, distance))
    { 
    Prop prop = hit.collider.gameObject.GetComponent<Prop>();
    
    if (prop != null && prop.showText)
    theGuiObject.enabled = true;
}