How to make a player fly properly

I’m making Thor’s hammer. Everything went well, except for the flying ability. When the mouse is pointed down, the player flips 180 degrees on the HumanoidRootPart’s Z-axis. This video may give you a clearer description (because I’m bad at describing):
robloxapp-20211020-1922022.wmv (1.9 MB)
I haven’t tried anything yet because I’m new to CFrame :). I have a shallow understanding of it. By the way, here’s my script:

spawn(function()
	while wait() and bodyVelocity and bodyGyro do
		game.Players.LocalPlayer.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Flying)
		bodyGyro.CFrame = CFrame.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, mouse.Hit.p)*CFrame.Angles(math.rad(-60), 0, 0)
		bodyVelocity.Velocity = (mouse.Hit.p - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Unit*50
	end
end)

There’s a function that triggers this flight ability but that isn’t important. In addition, the Body Gyro and Body Velocity have MaxTorque and MaxForce all set to math.huge.

2 Likes

You can try to check if the player rootpart pos and the mouse hit pos are 180 degrees using:


local diff = (pos1 - pos2);
local newAngle = math.deg(math.atan2(diff.X,diff.Z))


I found and learned this by a post on devforum,but idk if it works for your.

So goodluck and have a goodday.
:grinning_face_with_smiling_eyes:

1 Like

I just found out that this problem only occurs when Mouse Lock is on. When I print the “newAngle” variable, the outputs for when Mouse Lock is on and off are the same. Can you think of another solution :)?

1 Like

Ingore the shiftlock of your game,if you still want to keep it then find another solution.

1 Like

Ok :), I’ll create my own Mouse Lock mode and disable Roblox’s Mouse Lock :).

1 Like

Good luck my guy :).I’ll watch your progress

1 Like