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

How do I rotate an object based on where the camera is looking?

Asked by 5 years ago
Edited 5 years ago

Hi. I am a novice scripter wanting to make a mechanic that puts the player in a controllable bubble.

I have made most of the mechanic, however I am currently unable to finish the mechanic due to my lack of knowledge on how to rotate something based off of where the camera is looking

(So say for example I wanted to use W to move forward, I am currently not sure on how to make it go forward relative to the camera.)

If anyone could show me some methods on being able to achieve this I would greatly appreciate it.

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 years ago

You should use the LookVector of the cameras CFrame for forwards & backwards, and RightVector for Left and Right. (Simply use -RightVector for left, and -LookVector for backwards.)

If you'd add all these units up to a single vector, you'd be able to also move diagonaly.

Eg.

local Cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local Vel

game:GetService("RunService").RenderStepped:Connect(function()

    Vel = Vector3.new(0,0,0)

    if UIS:IsKeyDown(Enum.KeyCode.W) then
        Vel = Vel + Cam.CFrame.LookVector 
    end
    if UIS:IsKeyDown(Enum.KeyCode.S) then
        Vel = Vel - Cam.CFrame.LookVector 
    end
    if UIS:IsKeyDown(Enum.KeyCode.D) then
        Vel = Vel + Cam.CFrame.RightVector 
    end
    if UIS:IsKeyDown(Enum.KeyCode.A) then
        Vel = Vel - Cam.CFrame.RightVector 
    end

    Vel = Vel * Vector3.new(1,0,1) -- Removes 'flight' lol.

    --Add & do whatever you want to the value here.
end)

0
Thanks! This answered my question perfectly! Thank you for your help! Starfythewat -3 — 5y
0
rip rubenkan he didn't accept the answer User#24403 69 — 5y
0
Uhm... How do I do that? Starfythewat -3 — 5y
Ad

Answer this question