Rotating the player on two global axis instead of 3

As the title says I’d like to know how I can make the player rotate on only two axis for not make it having awkward positions as in this gif when I aim down or up.

I’ve been thinking about converting the local rotation to global and then evaluate if it’s facing down or up. If you have a better idea suggest one please.

runService:BindToRenderStep("rotate",Enum.RenderPriority.Camera.Value,function()
	char.PrimaryPart.CFrame = CFrame.new(char.PrimaryPart.Position,mouse.Hit.p)
end)

You can index individual axises and form your own Vector3 (which in your case you would be modifying the Y axis to be static) like so:

Vector3.new(Part.Position.X, Part.Position.Y, 100)

and you can create your own CFrame with that like so:

CFrame.new(Vector3.new(Part.Position.X, Part.Position.Y, 100), lookAtVector3)

Hope this helps :slight_smile:

1 Like

@internetExplorerchad Bit of a late response but this might work, I would recommend locking the Z axis I believe it is. I don’t remember how to do that off the top of my head but ill see what I can find.

Id try reading through this similar post.

similar post

1 Like

try this

runService:BindToRenderStep(“rotate”,Enum.RenderPriority.Camera.Value,function()

char.PrimaryPart.CFrame = CFrame.new(char.PrimaryPart.Position, Vector3.new(mouse.Hit.X, Character.Torso.CFrame.p.Y, mouse.Hit.Z))

end)

1 Like

Thank you for all your solutions. I can’t wait to test them once I get home.

If you don’t mind some vector math, you could also calculate the yaw and pitch of the character-to-mouse vector and rotate using those angles. That gives you some finer control of the rotations, allowing you to set min and max pitch angles (e.g. you could limit the player to only looking 80 degrees upwards or downwards). Both angles can be calculated this way:

local vector = mouse.Hit.p - char.HumanoidRootPart.Position
local yaw = math.atan2(-vector.X, -vector.Z)
local pitch = math.acos(vector.Unit:Dot(Vector3.new(0, -1, 0))) - math.pi / 2

Then after adjusting for minimum/maximum values, you could rotate the character every frame like so:

runService:BindToRenderStep("rotate",Enum.RenderPriority.Camera.Value,function()
    char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position) * CFrame.fromEulerAnglesYXZ(pitch, yaw, 0)
end)
1 Like