Documenting the dark corners of the Unity Engine.

User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
raycasthit2d [2021/05/05 06:57]
andrew900460 created
raycasthit2d [2021/05/05 16:57] (current)
Line 1: Line 1:
-''RaycastHit2D'' can be used in an if statement to tell if the raycast hit anything, or if it hit nothing.+====== RaycastHit2D ======
-Example+==== Casts Implicitly To a Bool ==== 
-<code>+RaycastHit2D implements the C# [[https://forum.unity.com/threads/c-beginner-tips-1-your-friend-the-implicit-bool.185761/|implicit bool]] operator. This allows it to be used in an if statement to check whether a hit is found. 
 +<code csharp> 
 +public static implicit operator bool(RaycastHit2D hit) => (Object) hit.collider != (Object) null; 
 +++++ Example | 
 +<code c#>
 RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection); RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
Line 9: Line 14:
 } }
 </code> </code>
 +This is also valid:
 +<code c#>
 +RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
 +bool rayHitSomthing = hit;
raycasthit2d.1620197842.txt.gz · Last modified: 2021/05/05 06:57 by andrew900460