Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by kenny1007 · Feb 05, 2016 at 07:33 PM · dooronmouseoverdoors

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);
         }
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ahndrakhul · Feb 08, 2016 at 10:29 PM 0
Share

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.

avatar image kenny1007 Ahndrakhul · Feb 09, 2016 at 12:43 AM 0
Share

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.

avatar image Ahndrakhul kenny1007 · Feb 09, 2016 at 12:50 AM 0
Share

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.

Show more comments
avatar image Ahndrakhul · Feb 09, 2016 at 05:25 AM 0
Share

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?

avatar image kenny1007 Ahndrakhul · Feb 09, 2016 at 06:44 AM 0
Share

Correct, I prefer E for opening doors.

avatar image kenny1007 Ahndrakhul · Feb 09, 2016 at 05:39 PM 0
Share

So, if i use the second example as a separate script in the door object will it work?

avatar image Ahndrakhul kenny1007 · Feb 10, 2016 at 01:09 AM 0
Share

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

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HubDoor in 2D Game kit- Trying to make a door open after collecting 3 items 1 Answer

How do I not set a trigger to happen? 1 Answer

Opening/Closing doors 1 Answer

Open/Close Door only if player is near the Door 1 Answer

Trying to make a door. Zoning or Load Level with Animation. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges