Make OnMouse functions ignore GameObjects that pass in front of them

Hello,

I am presently attempting to make OnMouseExit() ignore objects that pass in front of the object clicked on. I have a GameObject in the scene that will respond to being hovered over and clicked on (essentially, this object is the ground). I also have other GameObjects in the scene moving around (bullets). The ground object has a script on it that detects whether the mouse is currently interacting with it:

public class MouseTarget : MonoBehaviour
{
	private bool hovering;
	private bool mouseDown;
	
	void Start ()
	{
		this.hovering = false;
		this.mouseDown = false;
	}
	
	void Update ()
	{
		//Debug.Log (this.mouseDown);
	}
	
	void OnMouseEnter()
	{
		this.hovering = true;
	}
	
	void OnMouseExit()
	{
		this.hovering = false;
		this.mouseDown = false;
	}
	
	void OnMouseDown()
	{
		this.mouseDown = true;
		
	}
	
	void OnMouseUpAsButton()
	{
		this.mouseDown = false;
	}
	
	public bool Hovering{get{return this.hovering;}}
	public bool MouseDown{get{return this.mouseDown;}}
}

The problem comes when one of the bullets passes beneath the mouse while it is currently clicked on the target. When that happens, OnMouseExit fires off, turning off mouseDown.

How do I prevent this behaviour? I want the mouse to essentially ignore the bullets.

Thank you for any assistance!

-J-

BUMP, so many questions asking about this and not one of the threads are answered. I have to assume there is no answer and try something else =/. I too am looking at a very similar problem, I need my OnMouse functions to ignore a fog of war layer.

You could create a layer for the specific group of objects and try raycast from camera for that specific layer. (efficiently ignoring the bullets obscuring the object). However i have no idea how you could detect when the hover leaves the object without raycasting every Update() call, which is kinda inefficient.

1 Answer

1

You could also not add a collider to it. On mouse functions will only work with a collider