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.



Wednesday, June 19, 2024

Redesigning The Forest Scene in The Hidden Labyrinth

 


When you make a first person survival horror game like mine, you need a lot of assets to be able to work properly in conjunction. Various (volumetric) shaders and other things simply have to play nice together to make that happen. Well, guess what. It turned out that my original forest scene (the one that I used to start it all) was glitched and no one could figure out why (Yep, not even the developers of some of the assets I'm using). All I can think of is that the scene was originally created in an older version of Unity. So, I decided to revamp the entire project to a brand new scene that works! (Making sure to back it up every time there's a change The assembly is still the same, so I can easily import the old things like prefabs for instance to the new scene. The new scene is also a lot bigger, not more complex but bigger. For the past couple of weeks I've been prototyping the new scene, slowly introducing the different assets into it that I need to work.

Everything is set up!

Great news. All of the assets I need in my game (not just the scene) are set up and work flawlessly. Now it's just a matter of designing the level and making the puzzle games in such a way that they're interesting and memorable to the player.

If you look close enough in the center, you can see the entrance to 'The Hidden Labyrinth'.

Level design is going to be a challenge. This is because the island where the forest is, is divided into several sections that the player has to go through in order to reach (find) the entrance of the labyrinth. The hard part of a game developer in this regard is to strike a balance. The puzzles should be engaging, yet not too challenging to a point where the player gives up. Making it too easy I feel has the same effect. These puzzle sequences need some feedback, since this is my first time designing such elements in my game. I'll post frequent updates on socials and ask what you think of each puzzle and if it's too obvious or if I should make it a little bit more harder for players to solve.

Stay tuned for more updates about 'The Hidden Labyrinth' soon...


Tuesday, April 16, 2024

Mamba Classic 2 launch!

 

The moment has arrived! Today is the day you can download and play Mamba Classic 2 for absolutely FREE on itch.io! 

Don't forget to read the full set of tips and tricks in this blog post if you haven't already done so. It was a fun project to work on and hopefully you'll enjoy it as much as I had creating it. 

Any questions, feedback or suggestions: leave it on the itch.io page or drop me a line on X!

Click here to see the entire devlog for this game.


Friday, March 22, 2024

Mamba Classic 2: Rules of the game + tips

Hey guys! The snake game Mamba Classic 2 has entered the final stages of development and the rules of the game are simple and set! You'll probably not be surprised to find out they veer off a bit from the traditional snake games you're accustomed to: collect food and grow and see how far you can get without colliding with yourself. That's not the case here.

Mamba Classic 2 is a snake game has a lot more to offer and has a lot more features to help you rake in more score!


A casual game of Mamba Classic 2! In the Garden...


The set rules are as follows:

  • Your snake starts in the center of the screen (single player) and automatically moves in any direction and has 3 nodes + head, and must grow 16 nodes initially to make it solidify into a wall. After that it varies anywhere between 8 and 16 food to eat. Jewels or bonuses do NOT make your snake grow! After solidification the snake becomes only 1 node(tail).
  • Your snake can travel in ANY direction, including the reverse direction, which makes it easier to collect jewels or bonuses in walls that are obstructed by other walls behind it. You can 'collide' with yourself or other players. Walls are your biggest enemy! Touch them once and it's game over!
  • After each solidification a jewel will appear in a wall on the playing field in a random location, as well as a bonus, also in a random location. Getting the bonus will increase the jewel multiplier.
  • Jewels and bonuses won't stay on the map forever, after about 8 seconds they'll disappear! It's important to focus on getting the bonuses (especially early in the game), so you can increase the multiplier for the jewels.
  • The jewel value is always the fruit value multiplied by the bonuses. Do what you can to get the bonuses first, even if it's tempting to get a near jewel!
  • After eating 20 fruits, the speed level increases (up to 10)!
  • While playing Mamba Classic 2 the game can NOT be paused on any level! Score must be accumulated in one session.
  • After you hit a wall it's game over and a result screen will appear that shows you how many fruits, jewels and bonuses you've collected during that session. Your score will be recorded after you have entered your name!
  • If you don't provide a name, your name automatically defaults to 'Player1', and your current score will be recorded in the Top Ten list.
  • Bonus value is always 10 times the fruit value! This can increase your score significantly if you get those bonuses!
  • The starting value of fruit depends on the level. 10 for the first level (Garden), 50 for Level 2 (Glacier) and 100 for the final level (Palace). Speed increases considerably with each level.

A few tips to help you rake in more score:
  • Try to solidify your snake in Mamba Classic 2 as close to the center of the screen as possible! The random solidification after the first time makes this hard to gauge, but it can be done! It makes it easier to collect jewels and bonuses later if the solidification happens as far from the outer wall as possible!
  • Focus on collecting bonuses early on, they matter more than jewels. In later stages in the game jewels become more important to collect since they have a higher score attached thanks to the multiplier value.
  • Remember you can go the opposite direction and collide with yourself. Use this to your advantage to collect those pesky jewels in the wall or get yourself out of those nasty corners!
  • Later on in the game it might be hard to see all that's going on, however bonuses always make a special sound. When you hear the sound focus on spotting the bonus somewhere on the Mamba Classic 2 playing field!
  • Train your fingers to make turning corners quickly a breeze! 

A few general tips:
  • You can change the color of your snake for both players in the options menu (limited color scheme due to contrast issues that may arise from it).
  • You can turn retro mode on or off (scan lines), also in the options menu.
  • You can play the game in windowed mode (new), this can ONLY be toggled on or off (full screen) in the options menu.
  • You can give Dark Mode a try, this was actually an experimental feature in Mamba Classic 2 that I left in for fun! You can turn this either on or off in... you guessed it, the options menu. (Dark Mode resets every time you exit the game).
  • Don't name your snake 'Mamba', you'll regret it.
You can download Mamba Classic 2 on the official release site when available!

~Happy gaming!