How do I turn a player on the spot

I want to be able to turn the player and then walk in that new direction when pressing W.
code so far is

local function OnInputBegan(InputObject, WasProcessed)
	if WasProcessed then return end
	local keyPressed = InputObject.KeyCode
	if keyPressed == Enum.KeyCode.Q then
		print("Q Pressed")
		HRP.CFrame = (CFrame.new(HRP.Position) * CFrame.Angles(0,15,0))
		HRP.Orientation = Vector3.new(0,15,0)
	else if keyPressed == Enum.KeyCode.E then
			print("E Pressed")
			HRP.CFrame = (CFrame.new(HRP.Position) * CFrame.Angles(0,-15,0))
			HRP.Orientation = Vector3.new(0,-15,0)
		end
	end	
end

UIS.InputBegan:Connect(OnInputBegan)

What happens is that the turn appears to work but when I press W to walk forwards the player walks backwards.
I have been seaching for a solution but not found one that works the way I want.
I have aslo noticed that if I press Q several times the player goes downards into the ground.
I can continue expermenting and maybe find a solution but wanted to ask here in case someone else has down this type of player movement before.

Haven’t tested and without screenshots/videos, I no idea how the bug works and your explanation is not enough for me to understand what you are trying to achieve. But I can tell one mistake from your code that might solve your issue:

  1. CFrame.Angles use radians. Use math.rad()
  2. Do not change the Orientation after using CFrame.Angles(). It already changed the orientation. Doing so is either a waste or would tamper with the wanted rotation.

PS: Booted up studio to test and your code appears to be faulty if I understand your intentions correctly.

Creating a new CFrame from a Vector3 returns a positional CFrame with rotational axis being set to default (0). Your code, instead of rotating the character left and right, is setting the character’s rotation to a fixed angle, that being 15 degrees and -15 degrees.

If you want your character to turn left and right based on their rotation at the time of Q and E being pressed, you need to keep their original CFrame and multiply it with a CFrame.Angles.

local function OnInputBegan(InputObject, WasProcessed)
	if WasProcessed then return end
	local keyPressed = InputObject.KeyCode
	if keyPressed == Enum.KeyCode.Q then
		print("Q Pressed")
		HRP.CFrame *= CFrame.Angles(0,math.rad(15),0))
	elseif keyPressed == Enum.KeyCode.E then
		print("E Pressed")
		HRP.CFrame *= CFrame.Angles(0,math.rad(-15),0))
	end	
end
2 Likes

Thank you very much.
That fixed it and after adusting the value I have exactly what I need.
And…I dont drill my player into the ground anymore. :wink:

1 Like