Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


raycasthit2d

This is an old revision of the document!


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. 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:

RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
 
if(hit) {
    Debug.Log("yaaay I hit something!");
}

This is also valid:

RaycastHit2D hit = Physics2D.Raycast(someStartPoint, someDirection);
 
bool rayHitSomthing = hit;
raycasthit2d.1620198718.txt.gz · Last modified: 2021/05/05 07:11 by andrew900460