Help on raycasting

I can’t seem to understand raycasting. I have looked at the documentation but still can’t seem to wrap my head around it.

So, I am trying to make a gun so I choose raycasting so that it can create a raycast straight from the character to like 100 studs or so. So that if something is in the ray cast it should damage it.

So, what exactly is your question? If you’ve tried code that doesn’t work, please share it!

Would you like an explanation of how it works, or support on a script? We can’t really see what you want here.

Well hello theere.

I am gonna trryy explain raycasting to you.

First lets learn about how raycasting works.

Raycast or ray is like a irl ray. It has one fixed point and it can extend infinitely in fixed direction.

Whenever you start a raycast you give it a start or Origin to start at. That is a vector3 where it starts. Now it cant be static at one point can it be? So we give it a direction which is also a Vector3.

Now lets imply it in our code shall we?

game.Workspace:Raycast() --this is how you call a raycast

Now its blank so we fill it.

game.Workspace:Raycast(Origin,Direction*MaxDistance)

Let me explain the Parameters:

Origin: as described above the start.
Direction:The Direction you want it to travel to.
MaxDistance(optional/not needed): basicaly if you just set the direction it just goes on infinitely which is not ideal at all since its unneccesary in most cases. So we Mulgiply the vector 3 with a maxdistance(how manut studs do u want it go) so it limits itself instead of going forward infinitely.

Now this the Last Parameter which is Completely Option Yet its very useful.

RaycastParams.
This is a datatype which is call using raycastparams.new()

Its purpose is to filter things in raycast.
Lets say u dont want the ray to detect some parts. You can use raycastparams to filter them out.

So to do that we can

raycastparams.new().FilterDescendants = {your objects (must be a array)}

Now lastly u provide it a enum

local RP = raycastparams.new()
RP.FilterDescendants = {your objects}
RP.FilterType = Enum.RaycastFilterType.Blacklist 

--or Enum.RaycastFilterType.Whitelist and it will do the opposite of Blacklist

Now lets come to the real part. The stuff that it returns.
Raycast returns many things but i will explain main ones.
Instance: the instance that it touched. Btw whenever a raycasts ray touches something it ends and returns this stuff.
Distance: distance of intersection from origin
Position: Vector3 of intersection
Normal: the normal of face of basepart if any
(Least Used)

Now for your part.(assuming u are using a tool)

local part = player.Character.HumanoidRootPart
local Tool = script.Parent
local RayParams = RaycastParams.new()
local Dir = Part.CFrame.LookVector * 100
RayParams.FilterInstancesDesendants = player.Character
RayParams.FilterType = Enum.RaycastFilterType.Blacklist

Tool.Activated:Connect(function()
local Ray = workspace:Raycast(part.Position,Dir,RayParams)
if Ray and Ray.Instance.Parent.Humanoid then
Ray.Instance.Parent.Humanoid:TakeDamage(10)
print("Hit")
end
end

Hope it helps

3 Likes

Thank you! I really appreciate you writing this all for me to understand ray casting you helped me a lot : )!

Welcome (Also Thanks for Giving me my First Solution :smiley: )