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

Raycasting please help?

Asked by
Voltoxus 248 Moderation Voter
9 years ago

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

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
0
thxs :D Voltoxus 248 — 9y
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) BlueTaslem 18071 — 9y
Ad

Answer this question