Bullet doesn't appear from the barrel when strafing

  1. What do you want to achieve? Keep it simple and clear!
    This is more of a visual thing instead of a gameplay issue, I want the bullet to come from the barrel of the gun.

  2. What is the issue? Include screenshots / videos if possible!
    Whenever the player is moving to the right or left, the bullet doesn’t come directly from the barrel of the gun. For example, when I am moving to the right, the bullet looks like it comes to the left and vice versa when moving to the left.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Below is my (pretty janky) way of fixing it, it kinda works, but it is not perfect because I have to manually tweek it so it looks like it is coming from the barrel. I just want to see if anyone has any ideas on a better way of doing it.

function fire(character, startPos, range, direction, gravity, timeUntilBulletAppears)
	local lastPos = startPos
	local startTime = tick()

	local tracer = getTracer()
	tracer.Position = startPos
	tracer.Transparency = 0

	local rayFilter = rayFilter(character)

	local moveDir = character.Humanoid.MoveDirection
	local rightVector = character.HumanoidRootPart.CFrame.RightVector
	local forwardVector = character.HumanoidRootPart.CFrame.LookVector
	
	local offset = 0
	local forwardDot = moveDir:Dot(forwardVector)
	local rightDot = moveDir:Dot(rightVector)
	
	if forwardDot > 0 then -- this section is what I want to be more optimized
		if rightDot > 0 then
			-- Moving forward and right
			offset = .3
		elseif rightDot < 0 then
			-- Moving forward and left
			offset = -.3
		else
			-- Moving only forward
		end
	elseif forwardDot < 0 then
		if rightDot > 0 then
			-- Moving backward and right
			offset = .3
		elseif rightDot < 0 then
			-- Moving backward and left
			offset = -.3
		else
			-- Moving only backward
		end
	elseif rightDot > 0 then
		-- Moving only right
		offset = .4
	elseif rightDot < 0 then
		-- Moving only left
		offset = -.4
	else
		-- Not moving
	end

	startPos = startPos + (rightVector * offset * 2) -- Adjusts the starting pos

	while range > 0 do
		local timeLength = (tick() - startTime)
		local currentPos = startPos + (direction * timeLength)
		currentPos = applyGravity(currentPos, gravity, timeLength)

		local distance = (lastPos - currentPos).Magnitude
		range = range - distance

		local ray = workspace:Raycast(lastPos, currentPos - lastPos, rayFilter)
		if ray == nil then
			ray = workspace:Raycast(currentPos, lastPos - currentPos, rayFilter)
		end

		if ray then
			local hit = ray.Instance
			currentPos = ray.Position
		end
		updateTracer(tracer, lastPos, currentPos)

		if ray then
			break
		end
		lastPos = currentPos
		task.wait()
	end
	task.wait()
	tracer:Destroy()
end
1 Like