Unity Raycast Example: Master Collision Detection In 2D And 3D

Robert Jul 13, 2026

In the world of game development, the Unity engine stands tall as a powerful and versatile tool. One of its standout features is the ability to perform object interactions using raycasting. Let's dive into an example of how to achieve this.

How to Use LayerMask in Unity Raycast (Beginner Guide)
How to Use LayerMask in Unity Raycast (Beginner Guide)

Raycasting in Unity is a process that allows objects to interact with other objects in the scene. It's particularly useful for actions like picking up items, shooting projectiles, or detecting collisions. To illustrate this, we'll create a simple example where a player can pick up objects by looking at them.

Unity Raycast Explained for Beginners (With C# Example Code)
Unity Raycast Explained for Beginners (With C# Example Code)

Setting up the Scene

commence by creating a new 3D Unity project. Then, add two important scripts to your scene: a FirstPersonCharacter script for player movement, and a ChibiCharacter script for a playable character model.

Simple Multithreading for Unity
Simple Multithreading for Unity

In the Unity editor, drag and drop your ChibiCharacter object into your scene. Position it at the origin (0, 0, 0). Next, create a new cube (GameObject -> 3D Object -> Cube) and name it 'PickupObject'. Resize it and change its color as desired. Position it near the character for collection.

Implementing the Raycast Mechanism

2D lighting in unity
2D lighting in unity

Now, let's add the raycast functionality. Right-click in your Unity editor, go to Create -> C# Script, and name it "RaycastPickup". Attach it to the ChibiCharacter object.

Open the RaycastPickup script in your preferred code editor. First, import the necessary namespaces:

```csharp using UnityEngine; public class RaycastPickup : MonoBehaviour { public Camera playerCamera; public LayerMask pickupLayer; void Update() { if (Input.GetKeyDown(KeyCode.E)) { PerformRaycast(); } } void PerformRaycast() { RaycastHit hitInfo; if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hitInfo, 5f, pickupLayer)) { if (hitInfo.collider.gameObject.CompareTag("PickupObject")) { PickUpObject(hitInfo.collider.gameObject); } } } void PickUpObject(GameObject pickupObject) { pickupObject.SetActive(false); // Add code here to handle picking up the object, e.g., add it to inventory } } ```

In this script, we initiate a raycast from the player's camera when the 'E' key is pressed. If the raycast hits an object in the 'PickupObject' layer (defined by the 'pickupLayer' variable), we call the 'PickUpObject' method to deactivate the picked-up object. Replace the comment with your desired functionality for handling picked-up objects.

a person using a laptop computer with an apple logo on the screen and a red coffee mug in front of them
a person using a laptop computer with an apple logo on the screen and a red coffee mug in front of them

Don't forget to set up your layers in Unity editor. Click 'Edit' in the menu, then 'Project Settings', and navigate to the 'Tags and Layers' section. Create a new layer called 'PickupLayer' and assign your 'PickupObject' to this layer.

Testing the Raycast

Back in Unity, drag your MainCamera from the scene hierarchy to the 'playerCamera' field in the RaycastPickup script component. Make sure your 'PickupObject' is set to the 'PickupLayer'.

°❀⋆.ೃ࿔*:
°❀⋆.ೃ࿔*:

Now, in the game view, position your character to look at the 'PickupObject', press 'E', and watch as the object is picked up and deactivated. That's the power of raycasting in Unity!

Mastering raycasting opens up numerous possibilities in game development. From here, you can expand this example to includeCollectibles, enemies, or even power-ups within your game world. Happy coding!

Watercolour Shader Demo (Unity 5)
Watercolour Shader Demo (Unity 5)
Beauty
Beauty
Terrain and Mesh Blending in Unity
Terrain and Mesh Blending in Unity
an image of a game screen with the text'quick game art tips'on it
an image of a game screen with the text'quick game art tips'on it
a mirror that has been broken in half on the floor with blue tiles around it
a mirror that has been broken in half on the floor with blue tiles around it
Unity-Shaders/README.md at master · igradeca/Unity-Shaders
Unity-Shaders/README.md at master · igradeca/Unity-Shaders
Unity Good Graphics Tutorial
Unity Good Graphics Tutorial
Creating A Stylized Waterfall in Unity: Part 2
Creating A Stylized Waterfall in Unity: Part 2
Materials Instances Done the Right Way  - Unity Tutorial
Materials Instances Done the Right Way - Unity Tutorial
Make Your 2D Sprites Look like 3D using Normal Maps and 2D lights (Unity Tutorial)
Make Your 2D Sprites Look like 3D using Normal Maps and 2D lights (Unity Tutorial)
80 LEVEL Article: Building Great Lighting in Unity, Jonathan Caro
80 LEVEL Article: Building Great Lighting in Unity, Jonathan Caro
📌 Unity Humanoid Avatar Mapping for 3D Character Rigging
📌 Unity Humanoid Avatar Mapping for 3D Character Rigging
Getting started with Unity Editor
Getting started with Unity Editor
Can you simulate waves in Unity? Create realistic water effects using wave equation - Unity Tutorial
Can you simulate waves in Unity? Create realistic water effects using wave equation - Unity Tutorial
Tunic
Tunic
Unity Healthbar Tutorial : UI & Script
Unity Healthbar Tutorial : UI & Script
How to make a Minimap in Unity
How to make a Minimap in Unity
Unity Tutorial - VARIABLES. Beginners Guide in C#
Unity Tutorial - VARIABLES. Beginners Guide in C#
3D Stylized Water with Refraction and Foam Shader Graph - Unity Tutorial
3D Stylized Water with Refraction and Foam Shader Graph - Unity Tutorial
Learn Unity Beginner/Intermediate 2026 (FREE COMPLETE Course - Unity Tutorial)
Learn Unity Beginner/Intermediate 2026 (FREE COMPLETE Course - Unity Tutorial)