Teach me about gun raycast

You can write your topic however you want, but you need to answer these questions:
I want to know how exactly gun raycasting works and how do i make a beam bullet travel to its end

I really dont got no issue

trying this the second time, the other guy confused me so i wrote him a solution markf or nothing

alright boys any help is appreciated

1 Like

I recommend using the FastCast module, it can get things done really quick and easy.

aight beet let’s see if this helps me

Next time you ask someone for help, dont make them your enemy, because you are not capable of understaning their explination. Next time actually put in effort and ask for help.

Another solution might be to research Raycasts, but well here.

To cast a ray you will need to get 2 Vectors, one is the Position of the RayCast, the second is the Direction. A simple raycast would be cast like so:

local rayOrigin = Vector3.new(0, 0, 0)
local rayDirection = Vector3.new(0, -100, 0)

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

Now you can also add an array filled with Instances as a parameter, FilterDescendantsInstances if for example would like to make glass. First detect once you hit glass then break it, but continue the RayCast.

Now to get the direction.

rayOrigin + rayDirection = rayDestination

If we re-write this we can get the rayDirection, because we will be first needing to get the BarrelPosition and then the MouseHit. So getting the direction would look something like this:

rayDirection = mouse.Hit.Position - Barrel.Position

Let’s make the Vector3 we retreive from above a Unit, so that the magnitude is equal to 1. Now let’s say we want the MaxDistance to be 10, then the rayDirection would be:

local maxDistance
local rayDirection = (mouse.Hit.Position - Barrel.Position).Unit * maxDistance

Now if we cast the ray with the script on top. We can get the Distance the rayCast reached. From the raycastResults:

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
raycastResult.Distance

Now we can also get where it hit, so raycastResult.Position.

Now let’s create a beam.

local beam = Instance.new("Part")
beam.Anchored, beam.CanCollide, beam.CanQuery = true, false, false
...
beam.Parent = workspace -- Folder for beams

We will need to first create a CFrame, which is positioned in the middle of the rayOrigin and the raycastHit and which would look towards the rayDestination. Though when it got no hits we want it to start from the rayOrigin and move out until it hits the MaxDistance:

-- Distance is either the Distance of the raycast or the maxDistance.
local Distance = (raycastResults and raycastResults.Distance) or MaxDistance
-- Now need to offset it to the half the size of the Distance.
beam.CFrame = CFrame.new((rayDirection * Distance/2 + rayOrigin), rayDestination)
-- Now size the beam to the Distance
beam.Size = Vector3.new(1,1 Distance) -- Other 2 values can be changed to your liking

Now the beam is directly positioned in the middle, in between both Vectors. Now we have created the Beam.

From here on I am not quite sure what you are trying to make, but a route you could take is as follows:
Do everything I have written above. Now you will send a message to the server, a remoteEvent and after which the server sends back a remoteEvent back through all clients. It will give the Player who cast the beam and the Start and End position. Now if the LocalPlayer ~= Player then you will render it.
Else if you shot it yourself you want to render is directly after you send the remoteEvent.

Now you want to get the raycastResult.Instance this is whomever or whatever you hit. Send this through another RemoteEvent to the Server, now let the server do any verifying to it’s liking and then damage said player.

3 Likes

also how do I get the part that the raycast hit?

It is mentioned above in the last part “raycastResult.Instance”

Also you mention you wanting some sort of beam bullet. You will have to do that later. First you get a system where the gun damages other players, then you can make whatever animation you want. In 99% of FPS games, the bullets do not actually deal damage, but there is a raycast-like system behind it

im aware, by the way i was doing raycastResult.Parent lol instead of the Instance