Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] What on Earth is a RayCast and How would I use it to create a gun?

Asked by 4 years ago
Edited 4 years ago

I was scouring the internet for an answer for my long-lasting question on: "How on Earth do I create a gun?" All answers show something called a RayCast. What is that?

At this point, I am very confused and to create better games, I want to learn how to create a firearm for the player to fight others.

All I really want is a script, with in-depth detail on how each line works. I'm not asking for a script, but really just an explanation.

Any help please? All help will be appreciated.

1 answer

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

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)-- your ray--
local part = Workspace:FindPartOnRay(ray) --put the ray you made here--

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)-- your ray--
local part = Workspace:FindPartOnRay(ray) --put the ray you made here--
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.

0
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! Sensei_Developer 298 — 4y
Ad

Answer this question