Why is my bullet not going in the correct direciton?

I’m working on rewritting some gun system, but the bullet goes in the wrong direction sometimes if i shoot.

Proof:

This is my source, i believe it’s in the Bullet.CFrame line.

local function CreateBeam(Barrel, Origin, Direction)
	local Bullet = Services.ReplicatedStorage.Assets.Bullet:Clone()
	local BulletBeam = Bullet.Beam
	local EndBeam = Bullet.EndBeam
	
	BulletBeam.Attachment0 = Barrel.Attachment
	
	Bullet.CFrame = CFrame.new(Origin + Direction / 2, Origin)
	Bullet.Parent = workspace.Weapon.Bullets
	
	Services.DebrisService:AddItem(Bullet, GunSystem.Configuration["LiveTime"])
end

I think it is working, but the beam is just moving as your character moves, so that’s why it looks off.

I think you need to adjust your beam length with the distance/magnitude of the ray too.

Try shooting in place and see if it works.

Yeah thats what i thought, it’s probably because of the character position, and the bullet can sometimes go up because the animation moves the weapon a little bit up so thats propably why. It’s the character but it seems that this line probably fixs it

I also changed the whole logic of the gun, this is the line i added.

Bullet.CFrame = CFrame.new(Barrel.Position, MousePosition) * CFrame.new(0, 0, -DistanceOfBullet / 2)

Is it working though? Your math doesn’t particularly seem off.

The bullet is actually going in the correct direction, but, like Ben pointed out, the beam is moving with the character.
image

Create a temporary attachment point at the position of the gun and set the Beam’s attachment0. Don’t parent the attachment to anything that moves.

I didn’t really understand what u meant, can you explain it in the terms of how things are named in my game?

This is the beam
image

This is the gun part where the beam is coming from
image

You’re setting the Beam to use the attachment on your gun’s barrel, which itself is attached to your character. When your character moves, your gun moves, the attachment moves and the bullet beam moves.

My suggestion is to create a temporary attachment at the position of your gun barrel at the time of the bullet’s creation. This temporary attachment should be parented to something that will never move.

local function CreateBeam(Barrel, Origin, Direction)
	local Bullet = Services.ReplicatedStorage.Assets.Bullet:Clone()
	local BulletBeam = Bullet.Beam
	local EndBeam = Bullet.EndBeam

	local TempAttachment = Instance.new("Attachment")
	TempAttachment.WorldCFrame = Barrel.Attachment.WorldCFrame
	-- Attachments must be parented to a Part or Terrain
	TempAttachment.Parent = workspace.Terrain
	
	BulletBeam.Attachment0 = TempAttachment -- changed
	
	Bullet.CFrame = CFrame.new(Origin + Direction / 2, Origin)
	Bullet.Parent = workspace.Weapon.Bullets
	
	Services.DebrisService:AddItem(Bullet, GunSystem.Configuration["LiveTime"])
	-- Delete temporary attachment
	Services.DebrisService:AddItem(TempAttachment, GunSystem.Configuration["LiveTime"])
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.