Shoot raycast2D from player to mouse

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