Personally I dont use raycasting to make guns, but they do have a variety of uses.
raycasting shoots a beam in a specific direction and will collide with objects that it "hits".
to use raycasting, first you must make the ray:
local ray = Ray.new()
this will create a ray but its not quite done yet. ray.new needs two paremeters, the first is the vector3 position were the ray will start, the second is the direction the ray will go:
local ray = Ray.new(game.Workspace.Part.Position,Vector3.new(5,0,0)
now that a we made this ray, we now need to cast it. you can "fire" the ray with this:
local part = Workspace:FindPartOnRay(ray)
putting it together it now looks like this:
local ray = Ray.new(game.Workspace.Part.Position,Vector3.new(5,0,0)
local part = Workspace:FindPartOnRay(ray)
now the ray is fired, and "part" is the part that the ray may or may not find.
if the ray does not find anything, "part" will be nil, otherwise, "part" will equal whatever basepart it hits.
local ray = Ray.new(game.Workspace.Part.Position,Vector3.new(5,0,0)
local part = Workspace:FindPartOnRay(ray)
print(part.Name)
you can of course use an if statement to filter out what part you want, but this should help explain how its used and what its doing.
1 comment
Thank you for answering! I'm curious on what other ways you can create a gun without rays. Do you have a more simpler way of making a gun for me? Thanks man!