Need help with CFrames

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;
RayCast = Ray.new(LocalPlayer.Character.Head.Position, Mouse.Hit.LookVector)

Here is a video demonstration of what I mean:

3 Likes

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
2 Likes

You’re not allowed to do this.

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.

2 Likes

Their join date doesn’t matter. They have to post for themselves, you can’t do this

Ok, well I’ll keep it in mind for next time.

It fixed the problem of direction, but all the bullets just go to 0, 0, 0.

It might be because it is hitting the bullet or the character try ignoring/ blacklisting it.

2 Likes

No, it is shooting towards 0, 0, 0.

I am talking about using my idea with your old code before using the code that @luya_yobanka told you.

2 Likes

Ok, I’ve tried that and it didn’t work :confused:

If I were you I would use the new ray cast and you can still get the ray cast hit position.

2 Likes

I already use the Hit.Position.

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.

2 Likes

No, that’s the problem. It works perfectly in certain directions, but in others it just goes in random directions.

Try setting the barrels cancollide to false so it doesn’t mess with the bullets trajectory.

2 Likes

There is no gun yet, it comes from the character’s head based on your camera rotation. And no the character doesn’t mess with it.

@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:

local OffsetDegrees = (((360 / BulletSplitMinimum) * Index) + math.random(-BulletSpread, BulletSpread));
local NewRayTarget = (CFrame.new(RayCast.Origin, -RayCast.Direction) * CFrame.Angles(math.rad(math.random(-BulletSpread, BulletSpread)), math.rad(math.random(-BulletSpread, BulletSpread)), math.rad(OffsetDegrees)) * CFrame.new(0, 0, Range));
local FinalRay = Ray.new(CFrame.new(RayCast.Origin, RayCast.Direction).Position, NewRayTarget.Position);
BulletsReturn[Index] = Bullets.CreateBullet(Player, FinalRay, Character);
2 Likes

Thanks, your prompt actually helped me fix a problem that came up later :slight_smile:
I realised that the two inputs to a Ray.new() were Origin and Direction, not Destination.

2 Likes

@TheSilverLeopard how did my reply help ?