Home > Blockchain >  Unity RaycastHit2D in Physics2D.Raycast not working
Unity RaycastHit2D in Physics2D.Raycast not working

Time:10-09

I'm using Unity and wanna create a 2D game. Now there's a problem. My problem is, that the third argument (out hit) is not working. Following error has appeared: "Argument 3 may not be passed with the 'out' keyword"

Vector3 dir = DirFromAngle(globalAngle);
        RaycastHit2D hit;

        if (Physics2D.Raycast(transform.position, dir, out hit, obstacleMask))

CodePudding user response:

The Physics2D.Raycast() does not have any "out hit" parameter - you are probably thinking about the 3D version Physics.Raycast() (which can't be used in a 2D game). Do something like this:

 RaycastHit2D hit = Physics2D.Raycast(transform.position, dir, float.MaxValue, obstacleMask);
 // If it hits something...
 if (hit.collider != null)
 {
    ...
 }
  • Related