Search Unity

Question Raycast2D / DrawRay cursor relative position does not update correctly

Discussion in 'Physics' started by MichaelABC, Jan 14, 2022.

  1. MichaelABC

    MichaelABC

    Joined:
    Jan 25, 2020
    Posts:
    69
    Hello folks!
    I have written this code to visualise a cursor/crosshair around my player that I will need to shoot some very particular projectiles later on.

    So I also need a raycast between the cursor and the player position, which I take from the main player script.

    However as you can see in the second image, when I debug with Debug.DrawRay (using the same variables as the raycast) and I move the player away from its starting point I notice that DrawRay does not draw all the way to the new relative cursor position anymore.

    What am I doing wrong?
    Any suggestion on how to solve?

    Code (CSharp):
    1. public class CursorXhair : MonoBehaviour
    2. {
    3.     public Player Player;
    4.  
    5.     // Start is called before the first frame update
    6.     void Awake()
    7.     {
    8.        Cursor.visible = false;    
    9.     }
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         Vector2 mouseCursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    14.         transform.position = mouseCursorPos;
    15.         Debug.DrawRay(Player.transform.position, mouseCursorPos);
    16.         RaycastHit2D Player2Mouse = Physics2D.Raycast(Player.transform.position, mouseCursorPos);
    17.     }
    18. }
    upload_2022-1-14_19-42-32.png
    upload_2022-1-14_19-43-5.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You're simply using the physics query wrong.

    If you were to look at the docs for raycast, you'd quickly see that it doesn't take two world positions. Linecast does however.
     
    MichaelABC likes this.
  3. MichaelABC

    MichaelABC

    Joined:
    Jan 25, 2020
    Posts:
    69
    Hi Melv. I understood what's happening: I have mistaken position for direction... I thought that could be a problem but then I saw it sorta work so I pushed forward, not quite how to solve these things.

    This is how I finally solved it:

    Code (CSharp):
    1.  void Update()
    2.     {
    3.         Vector2 playerPos = Player.transform.position;
    4.         Vector2 mouseCursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    5.         Vector2 direction = mouseCursorPos - playerPos;
    6.         transform.position = mouseCursorPos;
    7.         Debug.DrawRay(playerPos, direction);
    8.         RaycastHit2D Player2Mouse = Physics2D.Raycast(playerPos, direction);
    9.     }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    The code above won't detect things from player position to the mouse, it'll detect things out to infinity from the player position which doesn't seem what you want.

    Why not just change your original code to use Linecast?
     
    MichaelABC likes this.
  5. MichaelABC

    MichaelABC

    Joined:
    Jan 25, 2020
    Posts:
    69
    Drawing works, but collision is wrong for the reasons you mention.

    I just changed it to Linecast and it works perfectly. I did not use it before when you first mentioned because I did not know it and was afraid to use it, but turns out is very simple!

    Thanks again
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    So you know, the 2D physics system doesn't really have a raycast under the hood, it uses a linecast behind the scenes. In the case of an "infinite" raycast, it uses a super large linecast. :)
     
    MichaelABC likes this.
  7. MichaelABC

    MichaelABC

    Joined:
    Jan 25, 2020
    Posts:
    69
    Interesting! Also kind of makes sense when you think about it. I believe Raycast is just more popular because of all the shooty-shooty bang-bang games and tutorials.
     
    Last edited: Jan 17, 2022
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Most likely yes. In Box2D, any "raycast" you call has to get condensed down to filling out this simple structure which is just a query from point A to point B.
     
    MichaelABC likes this.