Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago
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

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
6 years ago

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)
Ad

Answer this question