Home > Blockchain >  Detecting if 2D Collider is touching Mouse Pointer from Other script in Unity 2D
Detecting if 2D Collider is touching Mouse Pointer from Other script in Unity 2D

Time:05-18

I am working on a project using Unity2D, the goal is to be able to reference other main scripts to gain information from. In this specific case, I need to detect if the mouse pointer is touching a collider, however, from a separate script.

Usually, I would be able to create a boolean, and on mouse over set it to true, on mouse exit set it to false, like this:

bool isHovered;

void onm ouseEnter() {
    isHovered = true;
}

void onm ouseExit() {
    isHovered = false;
}

However, in the script, instead of doing this for each individual script, I would like to reference another script, like this:

public GameManager g;

void Update() {
    if (g.IsTouchingMouse(gameObject)) { //Code }
}

But there's multiple problems with this. In my game manager class, I would need something like this

public bool IsTouchingMouse(gameObject g) { return value }

Which there is multiple issues with this, because I don't have a way to register the onm ouseEnter and onm ouseExit events for those objects on another script, and I don't have a way to store the values for every single gameObject to ensure this will work for every object without having to manually modify this script.

I'm looking for two things, #1, how can I detect mouseovers on objects from scripts who's parents are not that gameObject, two, are there any ideas on how I would go about creating a function to return this variable instantly?

CodePudding user response:

Somewhat annoying, but I figured out a solution a few minutes after posting. So I will share it here.

public bool IsTouchingMouse(GameObject g)
{
    Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    return g.GetComponent<Collider2D>().OverlapPoint(point);
}

What this code is basically doing, is making a function that takes a gameObject as an input, creates a vector2 based on the position of the mouse cursor in world space, and then returns weather or not the 2D collider that the object contains is actually touching this imaginary point, the variable "point" should be interchangable with any 2D world space location. I was pretty much overcomplicating the entire issue.

CodePudding user response:

Find two resources you like and import them into Unity's Assets and set their Texture Type to Cursor

enter image description here

Create a new script and mount it to an empty object. Open the script, create three public variables of type Texture2D, return to Unity and drag your favorite pointer map to the variable window.

enter image description here

To set the object label, we judge which pointer to change by customizing the label; so first set the label for the object. For example, the ground, I added the Ground tag to it, the wall added the Wall tag; the column added the Cylinder tag.

enter image description here

Write a method to change the mouse pointer, where the main API for changing the pointer is Cursor.SetCursor()

      void setCursorTexture()
      {
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//Define the ray pointed by the mouse in the game window
          RaycastHit hitInfo; //Information of ray collision
          if (Physics.Raycast(ray, out hitInfo))//Determine whether to hit the object
         {
            // switch pointer
            switch (hitInfo.collider.gameObject.tag)
           {
             case "Ground":
                 Cursor.SetCursor(groundCr, new Vector2(16, 16), CursorMode.Auto);
                 break;
             case "Wall":
                 Cursor.SetCursor(wallCr, new Vector2(16, 16), CursorMode.Auto);
                 break;
             case "Cylinder":
                 Cursor.SetCursor(CylinderCr, new Vector2(16, 16), CursorMode.Auto);
                 break;
             default:
                 Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);
                 break;
              }
          }
       }

Implement this method in the Update() method called every frame.

END (you can go to run the program) Thank you and hope to help you

  • Related