Help with Gun Tilting with turning a certain way

Hello,

I am currently having an issue with fixing this bug. I am trying to make it so that when the player walks forward the gun will tilt down, if they’re moving sideways to the left the gun will tilt left, etc. But right now, if they’re just walking like north the gun will point down or if they’re walking south it’ll just look straight and if they’re looking east it would be tilting right/left, etc. How can I fix this? I am also wondering how I can make it so if the player’s camera is turning it would also make the gun tilt right or left depending on where they’re turning.

Reference Video:

Current Code:

			local humanoidVector:Vector3 = Library.Shared.cardinalConvert(humanoid.MoveDirection)
			if humanoidVector.X < 0 then
				--/ Left --
				
				cfAngle = cfAngle:Lerp( (defaultCFAngle * CFrame.Angles(math.rad(-10),0,math.rad(-5))) * add,0.1)
			elseif humanoidVector.X > 0 then
				--/ Right --
				
				cfAngle = cfAngle:Lerp( (defaultCFAngle * CFrame.Angles(math.rad(10),0,math.rad(-5))) * add,0.1)
			elseif humanoidVector.Z < 0 then
				--/ Forward --
				
				cfAngle = cfAngle:Lerp( (defaultCFAngle * CFrame.Angles(0,0,math.rad(-8))) * add,0.1)
			elseif humanoidVector.Z > 0 then
				--/ Backward --
				
				cfAngle = cfAngle:Lerp(defaultCFAngle*add,0.1)
			end

This is the cardinalConvert Code:

	local angle = math.atan2(dir.X, -dir.Z)
	local quarterTurn = math.pi / 2
	angle = -math.round(angle / quarterTurn) * quarterTurn

	local newX = -math.sin(angle)
	local newZ = -math.cos(angle)
	if math.abs(newX) <= 1e-10 then newX = 0 end
	if math.abs(newZ) <= 1e-10 then newZ = 0 end
	return Vector3.new(newX, 0, newZ)

Thanks!

1 Like

If anyone knows it’ll be much appreciated!

did you ever find out how i really need this

1 Like

we can use movedirection and vectortoobjectspace
in their code it should be something like this

local camera = workspace.CurrentCamera
local humanoidVector:Vector3 = Library.Shared.cardinalConvert(humanoid.MoveDirection)
local lookVector = camera.CFrame.LookVector
local relativeMove = CFrame.new(Vector3.new(), lookVector):VectorToObjectSpace(humanoid.MoveDirection)
local cameraTurnRate = camera.CFrame.RightVector:Dot(humanoid.MoveDirection)

if relativeMove.X < 0 then
    --/ Left --
    cfAngle = cfAngle:Lerp((defaultCFAngle * CFrame.Angles(math.rad(-10), 0, math.rad(-5 - cameraTurnRate * 10))) * add, 0.1)
elseif relativeMove.X > 0 then
    --/ Right --
    cfAngle = cfAngle:Lerp((defaultCFAngle * CFrame.Angles(math.rad(10), 0, math.rad(-5 + cameraTurnRate * 10))) * add, 0.1)
elseif relativeMove.Z < 0 then
    --/ Forward --
    cfAngle = cfAngle:Lerp((defaultCFAngle * CFrame.Angles(0, 0, math.rad(-8 + cameraTurnRate * 10))) * add, 0.1)
elseif relativeMove.Z > 0 then
    --/ Backward --
    cfAngle = cfAngle:Lerp((defaultCFAngle * CFrame.Angles(0, 0, math.rad(cameraTurnRate * 10))) * add, 0.1)
else
    --/ Not moving --
    cfAngle = cfAngle:Lerp((defaultCFAngle * CFrame.Angles(0, 0, math.rad(cameraTurnRate * 10))) * add, 0.1)
end
2 Likes

this doesnt really help me too be honest i have no idea how he set everything up but thanks for trying

create a post instead of bumping old posts with your code of your try i can help you