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

How do I move the player's camera on the X/Z axis regardless of its rotation?

Asked by 10 years ago

Currently I'm working on a camera for something. I've tilted the player's Camera from a LocalScript using TiltUnits, and allowed the player to rotate their camera if they press Q/E. However, the issue I've ran into is the fact that I can't get the camera to move along the Z/X axis as I want it to (rather it's going forward into the ground). If I just add to the player's camera's CFrame with a Vector3 then the player's camera doesn't move in the direction that I need it too when it's rotated.

I've never really played around with CFrames and the Wiki is completely useless in every explanation of, well, anything other than members specific to an instance. I'm completely stumped.

Below is the code that I'm currently using with the line dealing with the camera's movement being on line 23.

local Camera            =   Workspace.CurrentCamera
    Camera.CameraType   =   "Scriptable"
    Camera:TiltUnits(-math.pi)

local Increment         =   0.5 

local RunService        =   game:GetService("RunService")
local UserInputService  =   game:GetService("UserInputService")
    local Keys          =   {}

RunService.RenderStepped:connect(function()
    local W, A          =   Keys[Enum.KeyCode.W] or Keys[Enum.KeyCode.Up], Keys[Enum.KeyCode.A] or Keys[Enum.KeyCode.Left]  
    local S, D          =   Keys[Enum.KeyCode.S] or Keys[Enum.KeyCode.Down], Keys[Enum.KeyCode.D] or Keys[Enum.KeyCode.Right]
    local Q, E          =   Keys[Enum.KeyCode.Q], Keys[Enum.KeyCode.E]
    local Shift         =   Keys[Enum.KeyCode.LeftShift] or Keys[Enum.KeyCode.RightShift]

    local MoveIncrement =   Shift and Increment*3 or Increment
    local Rotation      =   Q and 1 or E and -1 or not Q and not E and 0
    local X             =   A and -MoveIncrement or D and MoveIncrement or not A and not D and 0
    local Z             =   W and -MoveIncrement or S and MoveIncrement or not W and not S and 0

    Camera.CoordinateFrame  =   Camera.CoordinateFrame
                                *CFrame.Angles(0, math.rad(Rotation), 0)
                                *CFrame.new(X, 0, Z)

end)

UserInputService.InputBegan:connect(function(Input) Keys[Input.KeyCode] = true end)
UserInputService.InputEnded:connect(function(Input) Keys[Input.KeyCode] = false end)

Here's a video showing what it's currently doing. http://docs.google.com/file/d/0B0lWhWEVSgliV0hFNTVBM2IybHc

Here's a visual representation of what it's doing, versus what I need/want it to do regardless of the camera's rotation (with red being how it's currently moving and green being how I want/need it to go).

http://i.imgur.com/xamOmeG.png

Answer this question