Bullet is offset from the original position when the player moves

  1. What do you want to achieve?

I want the bullets to accurately fire from the starting position

  1. What is the issue?

Whenever I move, the bullets are offset by a wide margin depending on where I aim at

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I am aware that a similar post was made here Projectile Offset When the Player is Moving, but there wasn’t a definite solution regarding this problem. I am also aware that the bullet will be delayed on the server before it begins shooting, which I have made to run under the client for instant feedback.

The gray block is where the bullet should start. The green dot is created with the bullet’s position when finalized, and the red dot is created with the position of the gray block.

2 Likes

maybe the bullet position should change depending on the player’s velocity idk

3 Likes

That might be a bit difficult to implement since the bullets are not always in the same position consistently.

1 Like

I noticed that in first person, the problem no longer persisted. I would assume the problem resides in the following code that allows my character to look at my mouse, but if anyone thinks the problem is otherwise, feel free to let me know.

-- Character looks at mouse code
RunService.RenderStepped:Connect(function()
	local RootPos, MousePos = player.Character.HumanoidRootPart.Position, mouse.Hit.Position
	player.Character.HumanoidRootPart.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

Is this something minuscule that I should not worry about, or is there a better code where I can keep the character looking at the direction I am pointing at all times?

Could you show your code?

Sure, all the code is running under local script

local Bullet = ReplicatedStorage.InstanceReplication.Bullet:Clone()
		Bullet.CanCollide = false
		Bullet.Parent = workspace

		local start
		local cf
		local targetPosition

		start = Tool.BulletExit.Position
		targetPosition = mouse.Hit.p
		cf = CFrame.new(start, targetPosition)

		local offset = math.rad(math.random(-1, 1)) -- random offset between -1 and 1 degrees

		-- Convert the CFrame to Euler angles
		local rx, ry, rz = cf:toEulerAnglesXYZ()

		-- Offset the rotation by the desired amount
		rx = rx + offset
		ry = ry + offset
		rz = rz + offset

		-- Create a new CFrame with the offset rotation
		cf = CFrame.fromEulerAnglesXYZ(rx, ry, rz).Rotation

		local force = Instance.new("VectorForce")
		local attachment = Instance.new("Attachment")

		force.ApplyAtCenterOfMass = true
		force.RelativeTo = "World"
		force.Force = Vector3.new(0, Bullet.AssemblyMass * workspace.Gravity - 25, 0) -- Bullet Drop

		force.Attachment0 = attachment
		force.Enabled = true

		attachment.Parent = Bullet
		force.Parent = Bullet

		Bullet.CFrame = cf + start
		

		local cloneTestpart = Instance.new("Part")
		cloneTestpart.Position = Tool.BulletExit.Position
		cloneTestpart.Color = Color3.fromRGB(255, 0, 0)
		cloneTestpart.Size = Vector3.new(0.25, 0.25, 0.25)
		cloneTestpart.Anchored = true
		cloneTestpart.CanCollide = false
		cloneTestpart.Parent = game.Workspace
		
		local cloneTestpart = Instance.new("Part")
		cloneTestpart.Position = Bullet.Position
		cloneTestpart.Color = Color3.fromRGB(0, 255, 0)
		cloneTestpart.Size = Vector3.new(0.25, 0.25, 0.25)
		cloneTestpart.Anchored = true
		cloneTestpart.CanCollide = false
		cloneTestpart.Parent = game.Workspace
		
		Bullet.Position = (Bullet.CFrame + Bullet.CFrame.LookVector * (Bullet.Size.Z/2)).Position
		
		Bullet.Velocity = cf.LookVector * 500 -- Bullet Speed

This actually is not the case since you’re only using the velocity for the spawn position.

But, you should use fast cast. It’s a really good projectile module.

Oh really? I didn’t know velocity sets the spawn position, I thought it was used to give it movement in one direction, similarly to apply force. I also have tried fast cast before, but from what I remember last time, the projectile was very jittery when I tested it out. I will stick to programming this feature normally, but I will look into it again

When looking into this spawn position thing, I tried to remove the velocity so the bullet would float in place, but turns out it still has the same behavior as last time, so it’s probably not the velocity.

maybe set the bullet CFrame before doing the force?

That’s what the code is showing. The force you see that is labeled force is the bullet slowly floating down (aka bullet drop). The force that moves the bullet forward would be the velocity.

Sorry I meant like

startPos = origin + humanoidMoveDirection

This predicts where the player will be.

Velocity plus origin equals future position.

Here’s a good video:

2 Likes