So i’m trying to lock camera cframe to player head but player can rotate camera, but the player movement after this is weird. How to fix this bug? Help please
local runservice = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local hum = char:FindFirstChildOfClass("Humanoid")
local cam = workspace.CurrentCamera
local head = char:WaitForChild("Head")
local ts = game:GetService("TweenService")
local input = game:GetService("UserInputService")
local camcframe = cam.CFrame
HeadOffset = CFrame.new(0,0,-0.3)
local humanoidpart = char.HumanoidRootPart
local screenSize = cam.ViewportSize
local screenCenter = (screenSize * 0.5)
local angle
input.InputChanged:Connect(function(inp,gamepr)
if inp.UserInputType == Enum.UserInputType.MouseMovement then
angle = Vector2.new(mouse.X,mouse.Y) - screenCenter
end
end)
runservice.RenderStepped:Connect(function()
cam.CameraType = Enum.CameraType.Scriptable
local result = head.CFrame * CFrame.Angles(0,math.rad(-angle.X/22.5),0) * CFrame.Angles(math.rad(-angle.Y/5.625),0,0) * HeadOffset
cam.CFrame = cam.CFrame:Lerp(result,1)
end)
Its because the movement keys like WASD are relative to the position of the camera.
Example:
The camera is facing towards, you press W and the character goes towards relative to where the camera is pointing out.
When you press D, the character itself will try to rotate facing the Right direction relative to the camera. So the rotation of the character changed, meaning the camera position rotated to following the head, so now the D-Right direction became the W-towards direction, causing that if you hold D key, it will constantly shift directions causing the character to spin on its own axis forever, going nowhere.
Its because the orientation of the camera which causes the movement keys being relative to that rotation, and because the D and A keys dont make the character to walk sideways, instead it tries to rotate the character to face and continue to aim that direction (only if the camera didnt change its own rotation)
Another thing, you are using the CFrame of the Head to position the camera, and the Head could rotate by itself (due to animations) to the center pos/rot of the camera will be more unstable cause the head is “randomly” moving. It would work better if you set the CFrame of the camera to follow the HumanoidRootPart CFrame and offset it 2 studs up or something to place it where the Head is, in that way becomes more centered and less buggy… but, that wont fix the fact that A,D and S keys movements still rotate the character to face that orientation, causing the camera spinning loop and the infinite spinning of the character.
Possible approach could be, do not use the default movement controller, and create your own so the WASD keys actually move forward, backward, sideways instead of trying to auto rotate the character.
Could be a different way to avoid the auto character rotation, but I never tried it
According to you, I need to make my own movement
controller, but how can I make it and connect it to mobile thumbstick?
Creating your custom one its only a suggestion, Im not saying thats the only way to cancel the auto rotation of the character from the movement keys.
Actually check the behavior of your camera script with the mobile movement system which works different than the keys movement one. The mobile one makes the character go in circles and pointing different angles.
So, whatever choice you take you still need to take in consideration that mobile and keys doesnt behave the same.
You can use CAS to connect any controller to mobile
I have a idea, how about just limit the HumanoidRootPart Rotation?
1 Like
It’s worth trying it⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀