I have a mouse cursor that I want to react when it enters certain GameObjects
, for example if you MouseOver a flower and click, you pick it up, but if you mouseover an NPC and click, you start a dialog.
I got the basics down, but my problem is that as soon as a collider that I don't want my mouse to interact with, for example my player, moves in front it just stops working, I guess because the players collider gets in the way.
My MouseOver script (in below example I add a herb to inventory on click):
private void Update()
{
if (mouseOver && GameManager.manager.mainState == GameManager.MainState.PLAY)
{
if (Input.GetMouseButtonDown(1))
{
if (invMngr.AddItem(herbManager.herbId)) //check if room in inventory
{
Destroy(gameObject); //if there is room; Add item and destroy from world
}
}
}
}
private void OnMouseEnter()
{
Debug.Log("Mouse enter: " + gameObject);
mouseOver = true;
}
private void OnMouseExit()
{
Debug.Log("Mouse exit: " + gameObject);
mouseOver = false;
}
There are 2 things I don't like about my approach.
- I have to attach this Script to every
GameObject
that I want the mouse to react to, wouldnt it be better to just have 1 instance of the script in the Game Manager running at all times?. - I can't control which colliders it should react to, like when adding a
LayerMask
toRaycast
. Sure it only react if it hovers over aGameObject
that has the Script attached, but if players collider overlaps the flower, it just stops working.
How can I script a cursor so that I know what GameObject
I'm overlapping (I do that now but surely 1 instance of the script would be better?), and so that I can control which colliders (or Layers) it should react to and which it should ignore?