1

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.

  1. 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?.
  2. I can't control which colliders it should react to, like when adding a LayerMask to Raycast. Sure it only react if it hovers over a GameObject 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?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Majs
  • 607
  • 2
  • 7
  • 20
  • If you want just one instance of script to do that then you've got to use Raycast. Although I don't recommended raycast so that's why I didn't put a solution for this. I assume this is a 3D object with 3D collider so see #6 and the `MeshDetector` script from the duplicated question. – Programmer Jun 28 '17 at 20:58
  • You can make a public variable named `isClicked` and add inside the `MeshDetector` script. When there is a click, set it to `true` in the `OnPointerClick` function then set it to false after one frame in the `Update` function. You can access this variable from the GameManager script. – Programmer Jun 28 '17 at 21:01
  • Thanks didnt see this question. My game is 2D. But with the help of that answer you linked I should be able to piece a solution together :) Thank you! Should I remove this question? – Majs Jun 28 '17 at 21:39
  • Use #7 if you are using 2D collider with SpriteRenderer. Every function that works in #1 should also work in #7 so check out supported functions in #1. If you have a problem let me know. You don't have to delete this unless you are getting down-votes. – Programmer Jun 28 '17 at 21:55
  • @Programmer Like all your other solutions this works perfectly. Dont underrestimate how much beginners like me appriciate that you take the time to help out. You are awesome man! – Majs Jun 29 '17 at 09:43
  • Glad I was able to help you. – Programmer Jun 29 '17 at 09:54

0 Answers0