Shoot raycast2D from player to mouse

Hi!
for some reason i can’t get this working:
I have a player and want to shoot a raycast from its position towards my mouse when i click (in the end this shall check if an interactable object is hit).

my code for this so far is like this, sitting on my player:

//called when Fire1 is being pressed
void Attack()
    {

        Vector2 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 shot = new Vector2(transform.position.x, transform.position.y) - screenPos;

      RaycastHit2D hit = Physics2D.Raycast(transform.position, shot, interactRange, TestLayer);
        if (hit.collider != null)
        {
            Debug.Log(hit.collider.name);
        }

        Debug.DrawRay(transform.position, shot, Color.red);
    }

I’ve tried some other ways as well but for some reason the raycast seems to always shoot somewhere i dont want it to (always in the middle of the screen or somewhere diagonally where my mouse it not even close) :smile: To be honest, i have yet no clue about it but am learning it, so might be a dumb mistake i am doing here without seeing it x)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above to increase clarity.

When you compute a “delta” between two vectors, such as where you compute shot, you always want to be “destination minus source,” but you have inverted that above.

Additional interim variables can also be helpful, like this:

Vector2 playerPosition = transform.position; // lops off the Z, making it zero

Vector2 shot = screenPos - playerPosition;   // always subtract source from destination for directions

Of course, if the above script is not physically precisely located on the player’s “shooting base transform” (eg, his finger or gun muzzle), all bets are OFF.

1 Like

Thank you for your answer so far!

I have readjusted the things you said (including the code block :slight_smile: ).

void Attack()
    {

        Vector2 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 playerPos = transform.position;
        Vector2 shot = screenPos - playerPos;

        RaycastHit2D hit = Physics2D.Raycast(transform.position,shot, interactRange, TestLayer);
        if (hit.collider != null)
        {
            Debug.Log(hit.collider.name);
        }

        Debug.DrawRay(transform.position, shot, Color.red);
    }

The problem is, it is not just offset, but it will in the current state always shoot towards the middle of the screen. Feels like the mousePosition related to the camera is the problem here, but i can’t find any tutorial or smth like that that would show me a different way :smile:

Also tried to set the camera not by “Camera.main” but to define it on top and drag it in in the inspector, but with the same outcome

Ah, I bet you’re using a perspective camera, not orthographic.

If you’re using a perspective camera, you cannot pass mousePosition directly into Camera.ScreenToWorldPosition() because the .z component means something.

Steps to success:

  • change your camera to orthographic… OR

  • understand what the Z needs to be (see the docs) and set it!

1 Like

Thats it <3

Ahh there are so many things to learn which are related to each other :smile:
Thank you very much!!

1 Like

Indeed! I also want to applaud you for using Debug.Log() and Debug.DrawRay because that will go a long way to helping you move fast and understand what is happening.

Oh yes, let me offer you this one extra piece of advice, which applies to raycast and drawray and any other ambiguous methods:

Always use named arguments with ambiguous methods like Physics.Raycast() because they contains many poorly-designed overloads:

https://discussions.unity.com/t/758115/8

2 Likes