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

Next revision
Previous revision
Next revision Both sides next revision
raycasthit2d [2021/05/05 06:57]
andrew900460 created
raycasthit2d [2021/05/05 07:11]
andrew900460 added syntax coloring for examples
Line 1: Line 1:
 +====== RaycastHit2D ======
 +
 +==== Implicit Bool Casting Magic ====
 +
 ''RaycastHit2D'' can be used in an if statement to tell if the raycast hit anything, or if it hit nothing. ''RaycastHit2D'' can be used in an if statement to tell if the raycast hit anything, or if it hit nothing.
 +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.
  
 Example: Example:
-<code>+<code c#>
 RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection); RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
  
Line 8: Line 13:
     Debug.Log("yaaay I hit something!");     Debug.Log("yaaay I hit something!");
 } }
 +</code>
 +
 +This is also valid:
 +<code c#>
 +RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
 +
 +bool rayHitSomthing = hit;
 </code> </code>
raycasthit2d.txt ยท Last modified: 2021/05/05 16:57 by uninomiconadmin