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.