The movement script currently allows sprinting, flying, and boosting while flying. The camera remains locked to the player from behind with a small delay through lerping when rotating in any direction. This script is part of my Invincible inspired game where you are a viltrumite.
The issue is when the player collides with a part, often times the camera will move to the side or up or down, creating a relative offset, ruining the flying script. This will create passive movements, spinning, etc.
I have tried many solutions such as different methods of controlling the characters rotation, but nothing works.
Here is the part of my Movement.lua that controls the player rotation and movement, thanks ahead of time!
local camCF = workspace.CurrentCamera.CFrame
local lookVector = camCF.LookVector
local rightVector = camCF.RightVector
local upVector = camCF.UpVector
if (UserInputService:IsKeyDown(Enum.KeyCode.W) or joystickY > 0) then
moveDirection += lookVector
end
if (UserInputService:IsKeyDown(Enum.KeyCode.S) or joystickY < 0) and not boosting then
moveDirection -= lookVector
elseif (UserInputService:IsKeyDown(Enum.KeyCode.S) or joystickY < 0) then
boosting = false
currentSpeed = boosting and boostSpeed or normalSpeed
end
if (UserInputService:IsKeyDown(Enum.KeyCode.A) or joystickX < 0) then
moveDirection -= rightVector
end
if (UserInputService:IsKeyDown(Enum.KeyCode.D) or joystickX > 0) then
moveDirection += rightVector
end
if (UserInputService:IsKeyDown(Enum.KeyCode.Space) or buttonA) and not boosting then
moveDirection += upVector
end
if (UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) or buttonB) and not boosting then
moveDirection -= upVector
end
if moveDirection.Magnitude > 0 and (joystickMagnitude ~= 0 or controllerMagnitude ~= 0) then
targetVelocity = moveDirection.Unit * currentSpeed * math.clamp(joystickMagnitude + controllerMagnitude, -1, 1) * speedMultiplier
elseif moveDirection.Magnitude > 0 then
targetVelocity = moveDirection.Unit * currentSpeed * speedMultiplier
else
targetVelocity = Vector3.zero
boosting = false
currentSpeed = boosting and boostSpeed or normalSpeed
end
currentVelocity = currentVelocity:Lerp(targetVelocity, smoothing)
bodyVelocity.Velocity = currentVelocity
local camLookDirection = camCF.LookVector.Unit
local camUpDirection = camCF.UpVector.Unit
local rightDirection = camLookDirection:Cross(camUpDirection).Unit
camUpDirection = rightDirection:Cross(camLookDirection).Unit
local targetRotation = CFrame.fromMatrix(Vector3.zero, rightDirection, camUpDirection)
local currentPos = humanoidRootPart.Position
local targetCFrame = CFrame.new(currentPos) * targetRotation
local playerLook = humanoidRootPart.CFrame.LookVector
local camLook = camCF.LookVector
local playerYaw = math.atan2(playerLook.X, playerLook.Z)
local camYaw = math.atan2(camLook.X, camLook.Z)
local yawDifference = math.deg(playerYaw - camYaw)
yawDifference = ((yawDifference + 180) % 360 - 180)
local newCFrame = humanoidRootPart.CFrame:Lerp(targetCFrame, 0.2)
if flying and boosting then
local clampedYaw = math.clamp(yawDifference, -5, 5)
local rollAngle = math.rad(clampedYaw)
local rollCFrame = CFrame.Angles(0, 0, -rollAngle)
newCFrame = newCFrame * rollCFrame
end
humanoidRootPart.CFrame = newCFrame