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)