So I am currently trying to make bullet spread on a shotgun, but uh… the bullets just sort of do their own thing when RayCast.Direction > 0. I’ve tried to fix it in every way I could think of, at this point I’m just stumped and have no idea what to do.
Here is the code:
local Shots = 10;
for Index = 1, Shots do
local OffsetDegrees = (((360 / Shots) * Index) + math.random(-36, 36));
local NewRayTarget = (CFrame.new(RayCast.Origin) * CFrame.Angles(math.rad(OffsetDegrees), 0, 0) * CFrame.new(RayCast.Direction) * CFrame.new(0, 0, 10));
local FinalRay = Ray.new(RayCast.Origin, NewRayTarget.Position);
BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay);
end;
Your code has several errors. Working with Rays is more difficult than just using vectors. So instead of fixing it I decided to use vectors and from that make some changes to make it work properly.
--local RayCast = Ray.new(LocalPlayer.Character.Head.Position, Mouse.Hit.LookVector)
local origin = LocalPlayer.Character.Head.Position
local target = Mouse.Hit.Position
local Shots = 10;
for Index = 1, Shots do
local OffsetDegrees = math.random(-15, 15)
local NewRayTarget = CFrame.lookAt(origin, target) * CFrame.Angles(0, 0, math.rad(math.random(-90, 90))) * CFrame.Angles(math.rad(OffsetDegrees), 0, 0)
local FinalRay = Ray.new(origin, NewRayTarget.LookVector)
BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay)
end
He has been on the devforum since 2018, its just he didn’t do the intro thing. Now he has done it, it erased, how many posts he had read, how much time he has spent on devforums etc.
It’s because it doesn’t fly in the direction of the gun, try making it so it will fly in the direction of the gun. Try setting it to shoot at the barrels CFrame.
@detroit_unspecific You can close the post now, because I have fixed the problem. Also thanks to @SloppyBanana225, @luya_yobanka, @rottendogDkR for your help, bits of your solutions combined fixed the problem. In the end the code turned out to be:
Thanks, your prompt actually helped me fix a problem that came up later
I realised that the two inputs to a Ray.new() were Origin and Direction, not Destination.