Door Icons OnMouseOver

HI all,

I’m trying to add an icon of a closed door when i mouse over a door and an icon of an open door when it’s open. I can’t find any video tutorial online other than highlighting the door but i would really like to have an icon show in place of my crosshair.

Below is the script i’m using and i will appreciate any help.

using UnityEngine;
using System.Collections;

public class DoorScript : MonoBehaviour {

	public bool open = false;
	public float doorOpenAngle = 90f;
	public float doorCloseAngle = 0f;
	public float smooth = 2f;

	void Start () 
	{
		
	}
	

	public void ChangeDoorState()
	{
		open = !open;
		audio.Play ();
	}

	void Update () 
	{
		if(open) //open == true
		{
			Quaternion targetRotation = Quaternion.Euler(0, doorOpenAngle, 0);
			transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
		}
		else
		{
			Quaternion targetRotation2 = Quaternion.Euler(0, doorCloseAngle, 0);
			transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
		}
	}
}

Is your mouse cursor the crosshair or do you want to change a crosshair UI element? If you want to change the mouse cursor you can put something like this in your door script:

using UnityEngine;

public class DoorScript : MonoBehaviour
{
    public Texture2D openedDoor;
    public Texture2D closedDoor;

    bool isDoorOpen = false;

    void OnMouseEnter()
    {
        UpdateCursor();
    }

    void OnMouseExit()
    {
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    }

    void OnMouseUpAsButton()
    {
        isDoorOpen = !isDoorOpen;
        UpdateCursor();
    }
    
    void UpdateCursor()
    {
        if (isDoorOpen)
        {
            Cursor.SetCursor(openedDoor, Vector2.zero, CursorMode.Auto);
        }
        else
        {
            Cursor.SetCursor(closedDoor, Vector2.zero, CursorMode.Auto);
        }
    }
}

If you want to change a UI element crosshair then you could do it like this:

using UnityEngine;
using UnityEngine.UI;

public class DoorScript2 : MonoBehaviour
{
    public Image UICrosshair;  //UI image reference.  Add in the editor
    public Sprite crosshairSprite;
    public Sprite closedDoorSprite; 
    public Sprite openedDoorSprite;

    bool isDoorOpen = false;

    void OnMouseEnter()
    {
        UpdateCrosshair();
    }

    void OnMouseExit()
    {
        UICrosshair.GetComponent<Image>().sprite = crosshairSprite;       
    }

    void OnMouseUpAsButton()
    {
        isDoorOpen = !isDoorOpen;
        UpdateCrosshair();
    }

    void UpdateCrosshair()
    {
        if (isDoorOpen)
        {
            UICrosshair.sprite = openedDoorSprite;           
        }
        else
        {
            UICrosshair.sprite = closedDoorSprite;
        }
    }
}

I hope this helps!

Here are the crosshair change on crosshairover scripts. This code is pretty bad and I might edit it later.

using UnityEngine;

public class DoorScript : MonoBehaviour
{

    public float doorOpenAngle = 90f;
    public float doorCloseAngle = 0f;
    public float smooth = 2f;
    public float doorStopValue = .2f;

    public bool IsDoorOpen { get; private set; }

    void Start()
    {
        IsDoorOpen = false;
    }

    public void ChangeDoorState()
    {
        IsDoorOpen = !IsDoorOpen;
        GetComponent<AudioSource>().Play();
    }

    void Update()
    {
        if (IsDoorOpen && (transform.eulerAngles.y != doorOpenAngle))
        {
            if (transform.eulerAngles.y > (doorOpenAngle - doorStopValue))
            {
                transform.eulerAngles = new Vector3(0, doorOpenAngle, 0);
            }
            else
            {
                Quaternion targetRotation = Quaternion.Euler(0, doorOpenAngle, 0);
                transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
            }
        }
        else if (!IsDoorOpen && (transform.eulerAngles.y != doorCloseAngle))
        {
            if (transform.eulerAngles.y < (doorCloseAngle + doorStopValue))
            {
                transform.eulerAngles = new Vector3(0, doorCloseAngle, 0);
            }
            else
            {
                Quaternion targetRotation2 = Quaternion.Euler(0, doorCloseAngle, 0);
                transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
            }
        }
    }
}

And:

using UnityEngine;
using UnityEngine.UI;

public class DoorInteractScript : MonoBehaviour
{
    [SerializeField] GameObject currentlyHit;
    enum crosshairStates { defaultCrosshair, openedDoor, closedDoor};
    int crosshairState = (int)crosshairStates.defaultCrosshair;
    public Image UICrosshair;
    public Sprite crosshairSprite;
    public Sprite closedDoorSprite;
    public Sprite openedDoorSprite;
    public float interactDistance = 5f;

    void Start()
    {
        UICrosshair = GameObject.Find("crosshair").GetComponent<Image>();
    }

    void UpdateCrosshair(bool isDoorOpen)
    {
        if (isDoorOpen && crosshairState != (int)crosshairStates.openedDoor)
        {
            UICrosshair.sprite = openedDoorSprite;
            crosshairState = (int)crosshairStates.openedDoor;
        }
        else if(!isDoorOpen && crosshairState != (int)crosshairStates.closedDoor)
        {
            UICrosshair.sprite = closedDoorSprite;
            crosshairState = (int)crosshairStates.closedDoor;
        }
    }

    void SetCrosshairToDefault()
    {
        UICrosshair.sprite = crosshairSprite;
        crosshairState = (int)crosshairStates.defaultCrosshair;
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, interactDistance))
        {
            currentlyHit = hit.collider.gameObject;

            if (currentlyHit.CompareTag("Door"))
            {             
                UpdateCrosshair(currentlyHit.GetComponentInParent<DoorScript>().IsDoorOpen);                

                if (Input.GetKeyDown(KeyCode.E))
                {
                    currentlyHit.GetComponentInParent<DoorScript>().ChangeDoorState();
                }
            }          
        }
        else
        {
            if (crosshairState != (int)crosshairStates.defaultCrosshair)
            {
                SetCrosshairToDefault();
            }
        }
    }
}

Once again you need to add the sprites to the DoorInteractScript in the inspector. Also, in the start function of the DoorInteractScript, you need to change the “crosshair” part of GameObject.Find(“crosshair”); to whatever the name of your crosshair UI image is in the scene hierarchy.

I’m sure this is the answer i was looking for. The only problem i have is that i don’t know how to integrate your script into mine and make it one script. I’m relativity new here and even though i can understand the script i still don’t know how to put them both together. You think you can do this for me? if not i can figure it out eventually but it will take me some time.