- Home /
How do I detect when mouse passes over an object?
I've been loocking for this for a while, but all I've found was OnMouseOver() function. I don't konow why but this doesnt work anymore. Raycast doesnt wok either. I need that when the mouse is over an object, image is showed instead of the normal cursor Please help!
As a new user you $$anonymous$$UST choose an answer and click the "TIC$$anonymous$$" button, thanks.
i have a box collide but on$$anonymous$$ouseOver still doesn't work
Answer by clunk47 · Oct 02, 2013 at 09:36 PM
Be sure the objects you wish to detect have a Collider attached, and use a RayCast, or OnMouseOver.
You didn't specify your language so I'll give a JS and CS example.
JS Example of Raycast:
//Attach this script to your Main Camera, or an Empty GameObject.
#pragma strict
var ray : Ray;
var hit : RaycastHit;
function Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
{
Debug.Log(hit.collider.name);
}
}
C# Example on Raycast:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
Ray ray;
RaycastHit hit;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
print (hit.collider.name);
}
}
}
JS Example on OnMouseOver():
//Attach this to each object that will need to be detected. This is why I prefer Raycast, becasue with Raycast, you only need one instance of the script.
#pragma strict
function OnMouseOver()
{
print(gameObject.name);
}
C# Example on OnMouseOver():
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void OnMouseOver()
{
print (gameObject.name);
}
}
Still this one is the best answer, my bug was that I forgot to add a $$anonymous$$esh Collider to my object, from the Inspector
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
Ray ray;
RaycastHit hit;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
print (hit.collider.name);
}
}
}
I get a NullReferenceException: Object reference not set to an instance of an object On$$anonymous$$ouseOverTile.Update () (at Assets/Scripts/On$$anonymous$$ouseOverTile.cs:10)
$$anonymous$$ake sure your camera is tagged $$anonymous$$ainCamera. Do a null check as well.
using UnityEngine;
using System.Collections;
Camera mainCam {get {return Camera.main.GetComponent<Camera>();} }
bool mainCamFound {get {return mainCam != null;} }
Ray {get {return mainCam.ScreenPointToRay(Input.$$anonymous$$ousePosition);} }
RaycastHit hit;
bool didHitSomething {get {return Physics.Raycast(ray, out hit);} }
bool hitCollider {get {return hit.collider != null;} }
void Update()
{
if(!mainCamFound)
{
Debug.LogWarning("$$anonymous$$ain Camera not found!");
return;
}
if(didHitSomething && hitCollider)
{
Debug.Log("Hit - " + hit.collider.name);
}
}
The C# example of On$$anonymous$$ouseOver(); does not give me the object that I was looking at but rather the object I attached the script to. Any ideas?
Answer by Sisso · Oct 02, 2013 at 09:22 PM
These links have everything that you need:
http://docs.unity3d.com/Documentation/ScriptReference/Collider.html
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.html
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Answer by Ash-Blue · Jan 22, 2020 at 08:23 PM
There are 3 options to solve this problem. All of them will trigger the MonoBehavior method OnMouseOver
. In my case the issue was trying to detect the mouse via trigger area.
Use a collider (set to trigger false)
Use a collider (set to trigger true). You will need to set your physics settings to force triggers to be detected by raycasts. This is generally not a good idea as every trigger in your game will now intercept raycasts
Create a new layer called
Interactions
and set it to interact with nothing on the collision matrix. Create a collider and object as normal with the new layer. The important part here is that you have a script that emits an event on the object so other scripts can make use of the mouse events
In my case option 3 did the trick. I can now have invisible trigger detection areas without affecting any other system. It works for keyboard / mouse and could easily be extended to work with a gamepad. Option 1 and 2 both created a giant mess for me.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
New UI ~ Check for mouse-hover 1 Answer
Raycast Destroys player. 1 Answer
Make the player shoot towards a mouse click 2d Platformer 1 Answer
SideScrolling Mouse Pointer Aim - Help 0 Answers