Unity Raycast2d



  1. Physics2d Raycast Unity
  2. Unity Raycast2d

I'm using Unity to create a 2D platformer. For collision detection, I'm using raycasting to detect any possible collisions with specific collision layers, and adjusting the desired delta x & y values as appropriate. Here is my collision detection class. The 2D Raycast and Linecast methods take Vector2 for their position, not Vector3 as they are 2D geometry checks, not 3D. Also, RaycastHit2D is a struct and therefore is never null. If nothing is hit then its collider property of RaycastHit2D is null.

Hey, if you didn't already know, I'm currently working on an open world stealth exploration game called Farewell North in my spare time, which is available to wishlist on Steam!

Related Posts

Animating Rotations through Code in UnitySynchronizing Idle, Walk and Run animations with a NavMeshAgent in UnitySiren Song, Devlog #3: But With More DogsUnity: Working with Custom HLSL Functions in Shader GraphUnity: Accessing Private and Protected Fields from the Inspector

It’s a pretty common requirement in game development to need to know if a particular character or object is on the ground. For instance, your player may only be able to jump, attack, or interact while they are on the ground.

A common way to check this is to use a Raycast, which essentially allows you to detect if any physics bodies exist within an invisible line - the Raycast itself being that line.

Physics2d Raycast Unity

In this post, I’ll show you how to use Raycasts to detect if an object is on the ground. For the purposes of this post, ground refers to GameObjects that are on the Ground layer and have a Collider attached to them. I’ll be writing from the perspective of a Player object that represents a character, but the same technique works for any GameObject.

Unity Raycast2d

Setup

The first thing we’ll need to do is add a public LayerMask to our player script. LayerMasks allow us to filter collisions based on the colliding body’s Layer, meaning we can limit our RayCast to search only for objects on the Ground layer.

In the player script, add the following:

Now in the Unity editor, select your player and set the Ground Layer property like so:

Again, this post assumes you have a GroundLayer setup and all your ground objects are on this Layer. Adjust to suit your needs as necessary.

Using Raycasts

Raycast2d

With the LayerMask ready to go, we can now write our logic to determine if the player is on the ground.

In the same player script, we’ll add an IsGrounded function that performs the logic and returns a boolean - true if the player is on the ground, false otherwise.

What we’re doing here is creating a Raycast using Physics2d, in a downward direction using Vector2.down (shorthand for Vector2(0, -1.0f)). We limit the Raycast to the groundLayer so that only Ground objects are detected, preventing us from colliding with our own physics body or that of anything else beneath us.

You’ll also notice we limit the distance of the Raycast, which is important because if the player jumps in the air, there would still technically be groundLayer objects beneath them - we’re interested only if it’s directly beneath them. It’s important to note that the value used (1.0f in this case) is going to vary based on your setup, so you’ll want to tweak and debug to determine what value works for your game.

Once the Raycast is complete, we check if there were any collisions using hit.collider != null. If the collider is not null, that means there was an object found and we are on the ground.

Raycast2d

Now you’ll be able to perform checks in the rest of your player script to determine if the player is on the ground, and act accordingly. For instance, you could check if the player is on the ground before allowing them to jump:

Debugging

A useful trick for debugging Raycast checks if to use the DrawRay function of Debug. This allows you to visualize the Raycasts by drawing them out on the scene editor in Unity - not in the actual game itself.

To add DrawRay to the IsGrounded function above, you would do something like the following:

Now in the Scene editor of Unity, you’ll see a little green line on your player like the image below, allowing you to validate the Raycast:

This is handy because you can monitor your Raycast as your player moves around, and ensure that it’s size and positioning is correct.

Let me know if this post was helpful on Twitter @kylewbanks or down below!