Unity 2D Raycast tutorial

Raycast 2D function in Unity is part of Unity’s 2D physics engine. It interacts with 2D game objects that have 2D colliders attached to them. If you are looking for a 3D raycast tutorial, check out our other tutorial on Unity Raycast.

Video Tutorial

The syntax of 2D raycast is as follows

Physics2D.Raycast(Vector2 origin, Vector2 direction, float Raylength, int layerMask, float minDepth = -10f, float maxDepth);

The parameters it takes

  1. Origin point in Vector2
  2. Direction of the ray in Vector2
  3. Length or distance to which you need to cast the Ray.
  4. Layers to include and exclude.
  5. 2D plane is X Y plane. If you have objects placed in different Z values, you can specify the minimum and maximum depth value the raycast should take into consideration.

RaycastHit2D

Raycast2D does not return true when hit but it returns the Hit object. The details of the object are saved in a RaycastHit2D variable.

You need to assign a RaycastHit2D variable to get the hit data. If the variable is null, then the raycast did not hit anything.

Physics2D.Raycast Script

Let’s see a practical example of raycast.

  • Add two 2D objects to the scene. Say a triangle and a square.
  • Add 2D colliders to them
  • Add a new script to the triangle and name it raycast2dtest
  • Copy and paste the below code to your script.
2D raycast scene

using UnityEngine
public class raycast2dtest : MonoBehaviour
{
    RaycastHit2D hit;
    
    void FixedUpdate()
    {
         
         hit=Physics2D.Raycast(transform.position, Vector2.left);
                 
         Debug.Log(hit. Collider);
       
    }
}

Raycast in 2D can be set to detect its own Collider. You can enable this option from Edit>Project settings>Physics2d> Check Queries start in Colliders. If you want the 2D raycast to ignore the gameobjects collider from which the raycast is starting then uncheck this option.

Unity Physics 2D settings

2D raycast from Mouse position

To raycast from mouse position, you need to get the mouse position and convert it into world space from screen point. Unity’s camera class has this function.

Next, we can just cast a 2D ray from the mouse position in the direction we want. Depending on what input system you are using, getting the mouse position may differ. Use our tutorial on getting mouse position for both new and old input system.

Here is a sample code using the old input system

using UnityEngine;

public class raycast2dtest : MonoBehaviour
{
    RaycastHit2D hit;
    
    void FixedUpdate()
    {
        Vector2 mousepos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
         hit=Physics2D.Raycast(mousepos, Vector2.right);
                 
         Debug.Log(hit.collider);
       
    }

}

RaycastAll 2D

By default, raycast will return the first 2D collider it hits. But if you need to get all the colliders in the same direction, you can use Physics2D.RaycastAll . Here is how you can use it in code

using UnityEngine;

public class raycast2dtest : MonoBehaviour
{
    RaycastHit2D[] hit;
    
    void FixedUpdate()
    {
        Vector2 mousepos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
         hit=Physics2D.RaycastAll(mousepos, Vector2.right);

         if(hit.Length>0)
         {        
           Debug.Log(hit[0].collider);
         }         
       
    }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading