Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a better way to rotate a character 90/-90 degrees flat?

local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

function turnPlayer(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.A then
			player.Character.HumanoidRootPart.CFrame = 			player.Character.HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0) --Like this, but my code does not exactly rotate my character 90° flat.
		elseif input.KeyCode == Enum.KeyCode.D then
			--Same thing as the top code, except 90°, its -90°.
		end
	end
end

userInputService.InputBegan:Connect(turnPlayer)

So every time the A/D key is pressed, I want the character to rotate 90°/-90° degrees flat.

The problem is, every time they rotate, they always rotate to 85.23° (Around there)

Is there a way to accomplish this?

Thank you.

1 answer

✓ Accepted answer

Instead of rotating the character itself by 90 degrees, store the rotation as a variable, and rotate the character by it. Change this variable whenever the player pressed A/D and update the character.

local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local rotation = 0
function turnPlayer(input, gameProcessedEvent)
    local hrp = player.Character and player.Character.HumanoidRootPart
    if not gameProcessedEvent and hrp then
        if input.KeyCode == Enum.KeyCode.A then
            rotation = rotation + math.pi/2
            hrp.CFrame = CFrame.new(hrp.Position) * CFrame.Angles(0, rotation, 0)
        elseif input.KeyCode == Enum.KeyCode.D then
            rotation = rotation - math.pi/2
            hrp.CFrame = CFrame.new(hrp.Position) * CFrame.Angles(0, rotation, 0)
        end
    end
end

userInputService.InputBegan:Connect(turnPlayer)

This discussion is preserved as it was. New answers, comments, and votes are closed.