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

Both sides previous revision Previous revision
Next revision
Previous revision
raycasthit2d [2021/05/05 07:11]
andrew900460 added syntax coloring for examples
raycasthit2d [2021/05/05 16:57] (current)
Line 1: Line 1:
 ====== RaycastHit2D ====== ====== RaycastHit2D ======
-==== Implicit Bool Casting Magic ==== +==== Casts Implicitly To a Bool ==== 
- +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
-''RaycastHit2D'' can be used in an if statement to tell if the raycast hit anything, or if it hit nothing+<code csharp> 
-This is because C# has the ability to do implied type conversions, and Unity has made it so RaycastHit2D can be converted to a bool. Pretty cool. +public static implicit operator bool(RaycastHit2D hit) => (Object) hit.collider != (Object) null; 
- +</code> 
-Example:+++++ Example |
 <code c#> <code c#>
 RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection); RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
Line 21: Line 21:
 bool rayHitSomthing = hit; bool rayHitSomthing = hit;
 </code> </code>
raycasthit2d.1620198718.txt.gz · Last modified: 2021/05/05 07:11 by andrew900460