Tuesday, March 4, 2025

The Best Way To Handle Raycast in Unity C#

 

Raycasts in Unity3D are extremely powerful if implemented correctly. They can handle anything from checking ground to detecting objects in your game. I’ve come across many tutorials that implement this raycast feature, however most of them are unintuitive and not very expandable. Unity allows you to build pretty much anything. However, this creative freedom can also be a downside if you’re not careful. A feature that works is not necessarily the best way of doing something.



Interface Method

The same goes for raycasting. There are two ways of doing it the ‘right’ way, both explained here. One of them is by using an abstract class (my favorite). An interface is global, which means it can be accessed anywhere from the entire assembly. See it as a ‘master method’ that any script can access and is able to use. The deal is, in doing so the script must implement that interface. A contract between two scripts.

It sounds complicated but it’s actually really easy to understand. Once you have the system set up, it’s simple to expand upon. And you can use it for anything in your game. First, let’s start by setting up the interface. Create a new C# script in your favorite IDE and write:

Save it as IInteractable.cs in your sub folder ‘Interfaces’ in your scripts folder. That’s it! You’ve just created your first interface. You can name your interface whatever you want. By convention, it’s common to start with the letter I and then what you want it to do. In my case I want to interact with a game object, hence I chose to name my interface ‘IInteractable’. But if you want enemies in your game to take damage, name it IDamagable for instance.

Ok, now that we have our interface it’s time to set up our raycast. As I said before, this system is set up to detect game objects in the world. Let’s say from a first-person perspective. You have a crosshair in the middle of the screen. When the character detects an interactable object, the cursor turns red. The player can press a button to interact with the object.

You could theoretically write this code in your player controller script. However, I’d like to keep everything as modular as possible. So let’s make a new script that we can attach to the player. Let’s name it ‘ObjectDetector’. We want to shoot a raycast from the center of the player’s screen into the world. Therefore, we only need a reference to the player camera. Then, cache it. Let’s also make a boolean to quickly determine whether the player is detecting something.

Then in the update loop we can now create our raycast.

Basically, we’re going to see if the detected game object is implementing the IInteractable interface. If it does we can call the Interact(); method. This method can be in any script, no matter what it is. It can be a door, key, enemy, NPC anything you want! Use TryGetComponent instead of checking to see if another script is null first. TryGetComponent expects an ‘out’ parameter of the type you’re looking for. The cool thing is that it automatically does the null check for you, thus making the raycast code faster.

Abstract Class Method

You can also apply the same technique with an abstract class. Instead of an interface, you would call a method from the abstract base class.


And in the derived class:


To raycast again with the abstract class method:


TryGetComponent

Both techniques use the TryGetComponent(out) which returns a boolean, except one is looking for an interface, the other one for an abstract class. Both are very versatile since they can be implemented with any object in your game.

It doesn’t matter which one you choose. I personally prefer the Interface system and use it all the time in my own games. Simple and easy to set up! If you have any questions about the implementation of this system or perhaps you found an even better way of doing it, let me know in the comments.