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

How would I make new ray, make part on said ray, and detect collisions?

Asked by
KDarren12 705 Donator Moderation Voter
4 years ago
Edited 4 years ago

I'm slightly confused on how to go about raycasting a new ray with a part on it detecting collisions. Like a gun or something else. I can't provide much code, so I'm only asking for developer links.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
for i = 1,math.huge do
wait()
local pos = mouse.Hit.p -- I used this to detect the mouse in a 3d space when clicked, I'm not sure if it is able to be used here. Please correct me if I am wrong.
ray.new(plr.Character["Left Arm"], pos) -- Here is where I'm confused. I've seen how to do something similar in another question before, but I just do not understand it. I will use "pos" as a placeholder. I also know that "Left Arm" is not accurate.
end

I am grateful for any help in advance!

1 answer

Log in to vote
1
Answered by 4 years ago

I assume you are trying to make a raycasting gun. Here is a good tutorial: https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox

You can add a raycast loop to make it automatic or use a for loop to make a shotgun effect (add spread to the guns). Just make a variable that stores whether the mouse is down or not and make it fire/stop based on if the mouse is down or not. You can use UserInputService or ContextActionService to detect inputs such as the click of a mouse (or you could use whatever is on the tutorial).

Make sure you add debounces too! Without them, players would be able to spam click to fire at a fast rate.

0
Thank you very much, really appreciate your help! KDarren12 705 — 4y
Ad

Answer this question