I am making a flying script and I want the character to face the same way that the camera is looking at
the issue is that when I am using the normal solution, CFrame.lookAt my character stars going towards the camera for some reasons
https://gyazo.com/e333a35f1ba4e1463c1fea1d4a270331
I have seen a forum post that does half of what I want, it does not rotate the character on the X rotation
the lines are
local CameraRotation = math.atan2(-cam.CFrame.lookVector.X, -cam.CFrame.lookVector.Z)
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.CFrame.Position) * CFrame.Angles(0, CameraRotation, 0)
this is my full code
local player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local cam = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local idleAnim = Character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("FlyIdle"))
local forwardAnim = Character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("FlyForward"))
local flying = false
local canFly = false
Humanoid.StateChanged:connect(function(old, new)
if new == Enum.HumanoidStateType.Freefall then
canFly = true
elseif new == Enum.HumanoidStateType.Landed then
canFly = false
end
end)
UserInputService.InputBegan:Connect(function(key, chat)
if chat then return end
if key.KeyCode == Enum.KeyCode.Space and (canFly or flying) then
if not Character or not Humanoid or not Character:IsDescendantOf(workspace) or
Humanoid:GetState() == Enum.HumanoidStateType.Dead then
return
end
print("Pro")
if flying then
Character.Animate.Disabled = false
idleAnim:Stop()
forwardAnim:Stop()
Character.PrimaryPart:FindFirstChild("FlightForce"):Destroy()
elseif not flying then
Character.Animate.Disabled = true
for i,v in ipairs(Humanoid:GetPlayingAnimationTracks()) do
if tostring(v.Name) == "FallAnim" or tostring(v.Name) == "JumpAnim" then
v:Stop()
end
end
idleAnim:Play()
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = Vector3.new(0,0,0)
bv.Name = "FlightForce"
bv.Parent = Character.PrimaryPart
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + cam.CFrame.LookVector)
end
flying = not flying
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if flying then
print(flying)
local CameraRotation = math.atan2(-cam.CFrame.lookVector.X, -cam.CFrame.lookVector.Z)
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.CFrame.Position) * CFrame.Angles(0, CameraRotation, 0)
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = Vector3.new(0,0,0)
forwardAnim:Stop()
local movez = 0
local movex = 0
local moveUP = 0
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
movez += 60
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
movex -= 60
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
movez -= 60
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
movex += 60
end
if UserInputService:IsKeyDown(Enum.KeyCode.E) then
moveUP += 40
movez += 40
end
if UserInputService:IsKeyDown(Enum.KeyCode.Q) then
moveUP -= 40
movez -= 40
end
Character.PrimaryPart:FindFirstChild("FlightForce").Velocity = Character.HumanoidRootPart.CFrame.lookVector * movez + Character.HumanoidRootPart.CFrame.rightVector * movex + Character.HumanoidRootPart.CFrame.UpVector * moveUP
end
end)
in starter character scripts
I am trying to make it rotate the x to the correct direction.