Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


raycasthit2d

Differences

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:05]
andrew900460 Added title, and added another example
raycasthit2d [2021/05/05 16:57] (current)
uninomiconadmin
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>+<code c#>
 RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection); RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
  
Line 16: Line 16:
  
 This is also valid: This is also valid:
-<code>+<code c#>
 RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection); RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
  
 bool rayHitSomthing = hit; bool rayHitSomthing = hit;
 </code> </code>
 +++++
raycasthit2d.1620198329.txt.gz · Last modified: 2021/05/05 07:05 by andrew900460