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

How do I move the camera relative to the direction it's facing?

Asked by 4 years ago

I'm making a freecam script that moves with WASD, everything works fine but the MoveCamera function in my script is only made to move relative to the world space, I want to make the camera move relative to where the camera is looking at (just like how studio camera mouvements are based on where you look at). But the problem with that is I don't know how to make the camera move relative to where it's looking at so that's why I came here for help, I would really appreciate it if someone would help me with this :slight_smile: anyways here is the function that controls how my camera move each time you hold a key moves:


local function MoveCamera(x,y,z,positive) --print("Move!") x = x > 0 and x + FCspeed or 0 y = y > 0 and x + FCspeed or 0 z = z > 0 and x + FCspeed or 0 local CameraPos = Camera.CFrame.p local MoveVector = Vector3.new(x,y,z) local Directionvector = Camera.CFrame + MoveVector local CF = positive == false and Camera.CFrame - MoveVector or Camera.CFrame + MoveVector Camera.CFrame = CF FCspeed = FCspeed + 0.001 FCspeed = math.clamp(FCspeed, 0.001, 0.5) LastMouvementCall = tick() SERVICES.RUN_SERVICE.RenderStepped:Wait() end

And here is the full script:

local FCspeed,LastMouvementCall = 0.001 ,tick()
    local function FreecamControl(_,State,InputINFO)

        local function MoveCamera(x,y,z,positive)
            --print("Move!")
            x = x > 0 and x + FCspeed or 0
            y = y > 0 and x + FCspeed or 0
            z = z > 0 and x + FCspeed or 0

            local CameraPos = Camera.CFrame.p
            local MoveVector = Vector3.new(x,y,z)
            local Directionvector = Camera.CFrame + MoveVector
            local CF = positive == false and Camera.CFrame - MoveVector or Camera.CFrame + MoveVector


            Camera.CFrame = CF
            FCspeed = FCspeed + 0.001
            FCspeed = math.clamp(FCspeed, 0.001, 0.5)
            LastMouvementCall = tick()

            SERVICES.RUN_SERVICE.RenderStepped:Wait()
        end

        if InputINFO.KeyCode == Freecam_Controls.KEYBOARD.W then

            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.W) do

                MoveCamera(0,0,.1,false)

            end
        elseif InputINFO.KeyCode == Freecam_Controls.KEYBOARD.A then
            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.A) do

                MoveCamera(.075,0,0,false)
            end

        elseif InputINFO.KeyCode == Freecam_Controls.KEYBOARD.D then
            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.D) do

                MoveCamera(.075,0,0,true)
            end

        elseif InputINFO.KeyCode == Freecam_Controls.KEYBOARD.S then
            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.S) do

                MoveCamera(0,0,.075,true)
            end

        elseif InputINFO.KeyCode == Freecam_Controls.KEYBOARD.LIFT then
            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.LIFT) do

                MoveCamera(0,.075,0,true)
            end

        elseif InputINFO.KeyCode == Freecam_Controls.KEYBOARD.DOWN then
            while SERVICES.USER_INPUT_SERVICE:IsKeyDown(Freecam_Controls.KEYBOARD.DOWN) do

                MoveCamera(0,.075,0,false)
            end
        end
    end

    local function HeartBeat()
        if PlayerState:lower() == "freecam" then

            if tick() - LastMouvementCall >= .1 and FCspeed > 0.001 then
                FCspeed = FCspeed - 0.001
                print(FCspeed)
            end

            Camera.CameraType = Enum.CameraType.Scriptable
            --print("eeee")
            SERVICES.CS:BindAction("Freecam_W", FreecamControl, false, Freecam_Controls.KEYBOARD.W)
            SERVICES.CS:BindAction("Freecam_A", FreecamControl, false, Freecam_Controls.KEYBOARD.A)
            SERVICES.CS:BindAction("Freecam_D", FreecamControl, false, Freecam_Controls.KEYBOARD.D)
            SERVICES.CS:BindAction("Freecam_S", FreecamControl, false, Freecam_Controls.KEYBOARD.S)
            SERVICES.CS:BindAction("Freecam_^", FreecamControl, false, Freecam_Controls.KEYBOARD.LIFT)
            SERVICES.CS:BindAction("Freecam_|", FreecamControl, false, Freecam_Controls.KEYBOARD.DOWN)
        else
            SERVICES.CS:UnbindAction("Freecam_W")
            SERVICES.CS:UnbindAction("Freecam_A")
            SERVICES.CS:UnbindAction("Freecam_D")
            SERVICES.CS:UnbindAction("Freecam_S")
            SERVICES.CS:UnbindAction("Freecam_^")
            SERVICES.CS:UnbindAction("Freecam_|")
        end

    end

1 answer

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago

Try using LookVector.

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

0
Ok I'll try to see how I could apply it to my script MeshyOryx 2 — 4y
Ad

Answer this question