Why do you use raycast?

can anyone tell me why dev often uses raycast? I understand raycasting is used for guns and stuff but you can do the same with mouse.hit and mouse.target.

Mouse hit is a client event which the server can’t do, raycast/line trace/hit scan, whatever you want to call it is useful for so many reasons you might not even understand. Do some research you’ll see why it’s so important & useful for developers.

Mouse.Hit and Mouse.Target represent a small portion of what could be achieved using raycasting. Even if you did use Mouse.Hit/Target for the purpose you suggested you wouldn’t get the realism that raycasting can provide. Imagine you want bullet drop, or a trajectory that isn’t completely linear, raycasting helps to achieve that. Especially in terms of the bullet hitting an instance, where you can predict the bullet hitting that instance right before it hits it making it superior to events like .Touched.

Raycasts also allow you to create vectors which you can use to perform mathematical operations which are especially useful when trying to find certain pieces of information. For example, you can get the new trajectory of a grenade by using raycasting and the dot method to find the new angle at which the grenade is now travelling at.

There are quite a few uses for raycasts which make them extremely useful. Generally speaking they are a cheap solution to a lot of problems you might find as a developer.

4 Likes

Raycasting is an essential part of a developers kit and can be used in multiple ways.

To start with, lets discuss what a ray actually is. A ray is a line that can go on indefinitely, but will tend to just go from point A with a set length. This, as you said, can be used for guns. However, there are several benefits you get from raycasting, that you wouldn’t get otherwise. A list of these benefits include:

  • Increased precision when checking for things, such as enemies;
  • They are extremely efficient, from my knowledge the most you can fire at once without lagging the game is 6000 in a second, although the number is probably higher;
  • Access to more variables/ data which helps you more, i.e. orientation, unit, etc.

Lets use your gun point as an example as to why you should use rays, instead of mouse.target/ mouse.hit. If you were to program a gun to shoot with these variables, they’d have to be client sided. This means that the player could change these variables with exploits, meaning they’d have an aimbot or an auto killer. If we were to cast a ray with this information, however, even though we’re using the same data (mouse.target), we can use the ray to check if it has hit anybody, instead of relying on the client.

This then brings up an important topic which is learning how to process most of a game on the server. Before FE (Filtering Enabled), Roblox had a huge issue with exploiters, as everything was processed on the client, meaning players could give themselves infinite money, for example. Nowadays, we have ways to stop this from happening, and ways to help prevent it. Rays are one of these ways.

However, rays, as @PerilousPanther rightfully said, can be used for much more than checking if somethings been hit or not.

2 Likes

Sorry to bump this topic, but how do you move a part on a ray?

I believe you’d need to find the WorldRoot | Roblox Creator Documentation 's origin and direction and make the Part move in that direction until it hits the Vector3 RaycastResult.Position

1 Like