Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)
		

3 comments

0

Thanks! This answered my question perfectly! Thank you for your help!

0

rip rubenkan he didn't accept the answer

0

Uhm... How do I do that?

This discussion is preserved as it was. New answers, comments, and votes are closed.