2
\$\begingroup\$

I am making a topdown 2d game and want to get the x,y of mouse clicks so I can move the character to that location.

I am writing a CharacterController script which performs the mouse-click check and raycast in the Update function:

void Update () {
    if (Input.GetMouseButtonDown (0)) {
        RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, -Vector2.up);
        if (hit.collider != null) {
            Debug.Log (hit.point);
        }
    }

This runs whenever I click the mouse, but the if(hit.collider != null) condition is never met.

I've looked around for other solutions, such as Unity - Raycast not hitting to BoxCollider2D objects, but when I change my code to the one used in that answer:

RaycastHit2D hit = Physics2D.Raycast(
  Camera.main.ScreenToWorldPoint(Input.mousePosition),
  Vector3.back
);

then the if(hit.collider != null) block is always true, even if I click on empty space, and it always returns the same coordinate. To illustrate this, here is a video of me clicking the game view. The green area has a 2d box collider that is very closely mapped to its boundaries. You can see that the logged hit.point is always the same.

video is here - https://i.imgur.com/uhNp8wC.gifv

screenshot - screenshot

\$\endgroup\$
  • \$\begingroup\$ Can you tell me what is your camera setting? Orthographic/ Perspective \$\endgroup\$ – Shuvro Sarkar Feb 5 '18 at 2:57
3
\$\begingroup\$

From the video link you gave, it seems to me that your camera setting is Perspective. In a perspective camera mode if you convert the "ScreenToWorldPoint" it will not work as expected because it takes the "z" axis into account. This issue has been described nicely in this question. https://answers.unity.com/questions/331558/screentoworldpoint-not-working.html

Moving on to your actual question. You can do either of these two things.

  1. Change your camera to orthographic and use the following code. (Similar to yours one)

    if(Input.GetMouseButton(0))
    {
        // this will take the mouse position in screen co-ordinate(left down corner 0,0 and top up corner screen resolution
        // then convert it to a worldpoint co ordinate (Transform.position)
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
    
    
        RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero);
    
        //if anything is collided
        if (hit.collider != null)
        {
            print(hit.collider.name);
        }
    }
    
  2. You can change the z value of your mouse input manually from code (if you use the perspective camera.) as mentioned in the link I have shared above

N.B: Also make sure that you are using 2d collider instead of the 3d collider. Otherwise, RaycastHit2D won't work.

Hope that helps

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.