Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Raycasting please help?

How do you move the end of a ray left/right up/down ? I would be using this to create shotgun spread. I will be firing 12 rays how would I create a shotgun type spread with the 12 rays.

1 answer

✓ Accepted answer

To move the end of the ray around, you need to multiply it's position by an offset. In your case the end position is most likely mouse.Hit.p, yes? So what we need to do is provide an offset for the mouse.Hit. Things to do;

1) Make a variable for the minimum offset, and the maximum.

2) Use a numerical for loop to create the ray 12 times.

3) Use a local variable to define the end of the ray's position within the loop, this should be mouse.Hit multiplied by a CFrame with arguments of math.random methods from the minumum offset to the maximum.

local minO = 0 --minimum offset
local maxO = 20 --max offset
local shots = 12 --number of shots

for i = 1,shots do
	--Define the origin for the end position of the ray
	local orig = mouse.Hit
	--Define the offsets for each axis.
	local xO,yO,zO = math.random(minO,maxO),math.random(minO,maxO),math.random(minO,maxO)
	--Define the end position of the ray, using the offsets.
	local finish = (orig.CFrame * CFrame.new(xO,yO,zO)).p --.p will return a vector
	
	local ray = Ray.new(handle.CFrame.p, finish) --Assuming 'handle' is defined..
	wait(.1)
end

2 comments

0

thxs :D

1

Keep in mind that generating random numbers like this will have significant biases towards the axis (and it's not straightforward to avoid this)

This discussion is preserved as it was. New answers, comments, and votes are closed.