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);
}
}
}
Answer by Ahndrakhul · Feb 06, 2016 at 07:44 PM
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.
Answer by kenny1007 · Feb 08, 2016 at 02:13 PM
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.
Assu$$anonymous$$g you want to change the mouse cursor, you can use something like this:
using UnityEngine;
public class DoorScript : $$anonymous$$onoBehaviour
{
public float doorOpenAngle = 90f;
public float doorCloseAngle = 0f;
public float doorStopValue = .2f;
public float smooth = 2f;
public Texture2D openedDoor;
public Texture2D closedDoor;
bool isDoorOpen = false;
void On$$anonymous$$ouseEnter()
{
UpdateCursor();
}
void On$$anonymous$$ouseExit()
{
Cursor.SetCursor(null, Vector2.zero, Cursor$$anonymous$$ode.Auto);
}
void On$$anonymous$$ouseUpAsButton()
{
ChangeDoorState();
UpdateCursor();
}
void UpdateCursor()
{
if (isDoorOpen)
{
Cursor.SetCursor(openedDoor, Vector2.zero, Cursor$$anonymous$$ode.Auto);
}
else
{
Cursor.SetCursor(closedDoor, Vector2.zero, Cursor$$anonymous$$ode.Auto);
}
}
public void ChangeDoorState()
{
isDoorOpen = !isDoorOpen;
audio.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);
}
}
}
}
The public Texture2Ds are your mouse icon sprites and in the import settings texture type for them, they have to be changed to "Cursor" before they will work right.
This script also opens and closes the door on click. I don't know if you wanted that functionality, but it's there.
I tried the script but i can hear the door open and close sound but nothing happened. It did asked to update the script like if it's written in an old Unity version.
It asked to update because you are using "audio.play()" which is deprecated. I don't know why the doors won't open. I assume that your doors are prefabs?
Is the mouse cursor changing when you hover over the doors?
Also, are you calling the ChangeDoorState() method from somewhere else in your code when you click on doors? This would cause it to be called twice and would result in the door not doing anything.
Oh, I think I see now. You're making a FPS game with a permanent crosshair in the middle of the screen. In that case, you would want the second example from above and no mouse click door functionality since you are using E as the activate button?
So, if i use the second example as a separate script in the door object will it work?
No, it won't do what I think you want it to do. It seems like you aren't interested in mouse over, but crosshair over ins$$anonymous$$d. I'll edit my answer above since the code won't fit in a comment. It's not the best solution for a few reasons, but it will probably work