Display GUITexture on Raycast within specified distance

I have a door that can be opened with a mouse click within a specified distance.

I need code to display a GUITexture when the player is within the specified distance.

Here is my code for the door’s InputHandler:

using UnityEngine;
using System.Collections;

public class InputHandler : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetMouseButton(0))
        {
            Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayCastHit;
          
            if(Physics.Raycast(ray.origin, ray.direction, out rayCastHit, 2))
            {
                Door door = rayCastHit.transform.GetComponent<Door>();
                if(door)
                {
                    door.PlayDoorAnim();
                }
            }
        }
    }
}

I’m not clear about your intention with that GUITexture but isn’t enough to set a boolean where you play the animation and then manage the GUI construction in OnGUI() function?

The GUITexture is an icon that shows the player the door can be opened.

I’m really new to programming so that’s why I came here asking for help.

I will use this script on the door object (you need to have a collider on the door for the OnMouseOver to be triggered)

P.S. The code is not tested, just an guide to experiment.

using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour
{
    private bool doorIsOpen = false;
    private bool doorCanBeOpen = false;
    private float openDoorDistance = 2.0f;

    public Texture2D doorIcon;
    public GameObject player;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void OnMouseOver()
    {
        float distanceToDoor = Vector3.Distance(player.transform.position, transform.position);
        if (distanceToDoor <= openDoorDistance  !doorIsOpen)
        {
            doorCanBeOpen = true;
        }
    }

    void OnGUI()
    {
        if (doorCanBeOpen)
        {
            GUI.Box(new Rect(0,0,Screen.width,Screen.height), doorIcon);
        }
    }
}

Thank you for your support! I tried it out but I think I’m doing something wrong - what should I be selecting to represent the Player object?

Your player :slight_smile: I guess you are doing a 3D game with at least a 3rd or fps controller. Thats the player and you can attach to him the special tag “Player” in inspector

So I tried that and it still doesn’t seem to work. I don’t mean to be a pain in the ass but should I have been using a different type of texture in this case? I set the private bool doorCanBeOpen to true to test, and I get the icon along with a dark grey rectangle on the screen… I was hoping for just the icon alone.

Thanks again for your help.

What I have done to get rid of the dark grey rectangle is I just used a GUI Label instead of the Box. I still can’t get the icon to display when I’m in range of the door though…

OK, sorry for the triple post but it seems to work for the most part now. The only thing is that the icon does not disappear after leaving the area. Any idea why this might be?

Thanks!

Set a boolean to true when you open the door and use it in OnGUI() function. This way when you close the door, you can set it back to false and the icon is going to be shown again.

    void OnGUI()
    {
        if (doorCanBeOpen  !doorIsOpen)
        {
            GUI.Box(new Rect(0,0,Screen.width,Screen.height), doorIcon);
        }

    }

Hmmmm, it still doesn’t seem to work right. Here is all of the code:

using UnityEngine;
using System.Collections;

public class DoorGUITrigger : MonoBehaviour
{
    private bool doorIsOpen = false;
    private bool doorCanBeOpen = false;
    private float openDoorDistance = 2.0f;

    public Texture2D doorIcon;
    public GameObject player;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void OnMouseOver()
    {
        float distanceToDoor = Vector3.Distance(player.transform.position, transform.position);
        if (distanceToDoor <= openDoorDistance  !doorIsOpen)
        {
            doorCanBeOpen = true;
        }
    }

    void OnGUI()
    {
        if (doorCanBeOpen  !doorIsOpen)
        {
			 GUI.Label(new Rect(0, 0, 94, 94), doorIcon);
    	}
}
}

Any ideas? :face_with_spiral_eyes:

Im no expert but try to take away the false statement at the start for initiation.

And then just replace it with it when you want it to be false for certain reasons like when you get out of there you can put it to false.

Like after { doorCanBeOpen = true} then put else { doorCanBeOpen = false;}

Not sure if it will work and im not home to try it but still =) go for it, that way it wont always be stated as false, although I dont see why it wont work for you because it shouldnt really matter.

PS: Do you get any reaction at all with the code you have?

If you find a hard time figuring things out here is a quick tip.

Use Debug.Log(“Its working”); or something of the like to see if a command goes of in your log. If it doesnt then something isnt working or the code doesnt run a part of your code that it should, or the if statement never goes through because things are always false etc etc.

Its wise to check that out to see if you get the debug statement atleast hen you can check if the test goes through and then you can try to check if there is something wrong with the actual display of the guy.

Update 2: Try to set the all the values in the start() method, doorIsOpen = false; there instead and for the rest of the tags, might help out.