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

How do i rotate a RotVelocity by the camera angle?

Asked by 6 years ago

To get the camera angles in a localscript, I have this code:

function cameraAngles()
    local x,y,z = workspace.CurrentCamera.CFrame:toEulerAnglesXYZ()
    return Vector3.new(x,y,z)
end

Then, I use the y of those angles to rotate the RotVelocity of a sphere:

script.Parent.RotVelocity = CFrame.fromEulerAnglesXYZ(0, cameraAngles.y, 0) * controls.Unit * 100

The controls records the input of the WASD Keys, and converts them into a vector3. Back in the localscript:

function getWasdKeys()
    local outputMovementVector = Vector3.new(0,0,0)
    for _,key in ipairs(UserInputService:GetKeysPressed()) do
        if key.KeyCode == Enum.KeyCode.W then
            outputMovementVector = Vector3.new(outputMovementVector.X + 1, 0, outputMovementVector.Z)
        end
        if key.KeyCode == Enum.KeyCode.S then
            outputMovementVector = Vector3.new(outputMovementVector.X - 1, 0, outputMovementVector.Z)
        end
        if key.KeyCode == Enum.KeyCode.A then
            outputMovementVector = Vector3.new(outputMovementVector.X, 0, outputMovementVector.Z - 1)
        end
        if key.KeyCode == Enum.KeyCode.D then
            outputMovementVector = Vector3.new(outputMovementVector.X, 0, outputMovementVector.Z + 1)
        end
    end
    return outputMovementVector
end

I think the problem is that the y camera angle reaches 0 at two opposite points.

Answer this question