Raycasting is a method of plotting the trajectory of in-game items, like bombs, lasers, explosions, and bullets. Though it isn’t entirely necessary to make a successful game, certain users have found ways to leverage the system to create innovative gameplay mechanics. We thought we’d explore the concept by talking to Daxter33, creator of the ever-popular Paintball!
Here’s how raycasting works: every weapon that features projectiles has a trajectory or, more simply, a path to travel. When you shoot a gun with instantaneous speed projectiles (as opposed to guns with slower projectiles that are physically simulated objects), you create a vector, which is determined by finding the gun in 3D space, the point it’s going to hit, and subtracting the two. Basically, you find a direction for your bullet to travel. A raycast can determine that path for you, as well as identify objects that appear in the path of the traveling bullet. You’re tracing a beam of light through 3D space.
Visualizing a ray for in-game purposes, such as drawing laser beams or creating paintballs, requires extra coding to animate the ray in 3D space. There’s no inherent visualization or graphics involved with raycasting, though many users have found ways to “trace” the paths by creating temporary parts in the same location as the ray. When you script a game that features speed projectiles, you’ve got to create a path for them to travel, and take into account the things they could potentially hit.
The path of a bullet is generally straightforward (literally). Say you program a gun that fires bullets. You raycast your environment, and it returns objects that that bullet could potentially hit (i.e. walls, trees, whatever may be in your game environment). Raycasting provides you with all the travel information of the bullet–the parts you hit and the coordinates of where you hit them. But the path of a bullet is usually very direct. It’s ejected from the barrel of the gun and moves straight down your line of site.
However, as we mentioned, raycasting can be used to create more interesting scenarios– like in the case of Paintball! by user Daxter33. His paintballs are not confined to a straight trajectory; they use gravity, physics, and a very complex raycasting model to act more like arrows than bullets. If you shoot one of the paintballs into the air, it will eventually fall, essentially cascading onto other players. Building these projectiles was a massive undertaking and has since changed first person shooters on ROBLOX for the better.
User Daxter33 explains his method:
At the time of creation, projectiles that most people were using were glitchy and often went through walls. The paintball gun is unique because it casts multiple rays. It has to, because my gun incorporates gravity-time and velocity. I’ve seen a lot of places use arc algorithms, but that’s unrealistic. It doesn’t follow the laws of gravity. The algorithm I developed is not an actual “formula” because shooting my gun is different every time. I can’t predict where a paintball will land at any given time. My bullets move as long as they can, but they are always moving downward, towards earth. It’s all about tilting–the angle of the bullet is in constant flux, changing as the bullet falls.
In order to achieve this, Daxter33 developed a complex raycasting system that updates itself every 1/30th of a second, constantly re-determining where the paintball is in 3D space. It’s a sophisticated weapon that shoots a new raycast every 1/30th of a second. His “Y” coordinate vectors drop as the bullets move forward. With a normal raycast, you fire (or create) a bullet, then it hits a target. It’s simple. With Daxter33’s weapon, he creates a bullet, then updates it with new coordinates as it flies through the air. It’s ingenious in its simplicity.
Raycasting can be used for a number of other aspects of game-building as well–several parts of games you’ve probably never considered are a result of proper raycasting. Grenades, for example, will kill you if within the blast radius. But if you’re behind a wall, the grenade should do little to no damage to you, right? In reality, this is true. But here’s the thing: when you code a game, you’ve got to cast rays in order to make walls a viable source of cover from explosions. Without raycasting, bombs would kill or damage you based on proximity, with no regard for your surrounding environment. You can also use raycasting to set parameters for what an explosion can and can’t penetrate. If a wall is a weak or thin, you may take a bit of damage, but if the wall is made of granite, you’re safe.
People have made all sorts of awesome things with raycasting, including laser trip mines, hovercraft, float pads and vampires that are afraid of the sun. We’re eager to see what users are able to come up with in the future. For now, keep experimenting with raycasting and keep us posted on what you’re able to create. If you want to learn how to build your very own laser gun using raycasting, here is a good place to start.