Rotating the player's camera to face the same direction as the HumanoidRootPart

  1. What do you want to achieve?
    When the player spawns I want their camera to face the same direction as their HumanoidRootPart is facing.

  2. What is the issue?
    No matter what I try, I cannot get the camera to rotate at all.

  3. What solutions have you tried so far?
    I’ve looked at some other topics and have found nothing of use.

Here is what I’ve tried so far, might be doing something completely wrong:

local Player = game:GetService("Players").LocalPlayer
local RootPart = Player.Character:WaitForChild("HumanoidRootPart")


Player.CharacterAdded:Connect(function()
	workspace.CurrentCamera.CFrame.LookVector = RootPart.CFrame.LookVector
end)
4 Likes

A h y e s

You must put the RootPart’s CFrame the same as the camera’s, to do this just simply put it through a loop using RunService:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local Cam = workspace.CurrentCamera

RunService.RenderStepped:Connect(function()
    RootPart.CFrame = CFrame.new(RootPart.Position, Cam.CFrame.LookVector)
end)

Not sure if LookVector would work, might have to do CFrame instead?

1 Like

image

This is the result, this just repeats itself over and over. I’m trying to set the Camera’s rotation not the HumanoidRootPart's rotation. Also not too sure why you’re using RunService to run the function over and over again.

1 Like

RenderStepped can be used to fire the event 1/60’s of a second (Maybe or more idk) which can be used to frequently change your RootPart’s Orientation

I changed it, could you try again maybe?

1 Like

LookVector is read-only, you cant assign to it. What you can do is use CFrame.lookAt to have the CFrame face in same direction as lookVector

1 Like

I only need to rotate it once when the player’s character is loaded (eg. every time they respawn)

1 Like

The only thing I have to say that others haven’t mentioned is that the CameraType has to be set to scriptable via

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

for any changes to the camera to take effect, but it would require more work since you’d have to create a custom camera system. So in the end, it may be better off rotating the RootPart instead.

Also for your question, I think you can just put this script in StarterCharacterScripts

local Character = script.Parent
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Cam = workspace.CurrentCamera

RootPart.CFrame = CFrame.new(RootPart.Position, Cam.CFrame.LookVector)

As it’s parented to the character when the character is added again, so you just do script.Parent I believe

1 Like

You’d do it like

camera.CFrame = CFrame.lookAt(camera.CFrame.Position, camera.CFrame.Position + hrp.LookVector)
1 Like

This doesn’t seem to work, is this trying to rotate the character or the camera?

1 Like

image
I’m getting this error.

1 Like
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

localPlayer.CharacterAdded:Connect(function(char)
  local lookVec = char:WaitForChild("HumanoidRootPart").CFrame.LookVector
  Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Camera.CFrame.Position + lookVec)
end)

This works. Put exactly this in StarterPlayerScripts as a LocalScript

1 Like

This works the first time the player spawns, but not if the player dies. I’ve had this problem before but couldn’t find any solutions. Are you having the same issue?

1 Like

oh yeah it’s supposed to be hrp.CFrame.LookVector

1 Like

Yeah, a simple fix is just to add a wait after the CharacterAdded, like this:

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

localPlayer.CharacterAdded:Connect(function(char)
	wait()
	local lookVec = char:WaitForChild("HumanoidRootPart").CFrame.LookVector
	Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Camera.CFrame.Position + lookVec)
end)

Hope this works.

9 Likes

Now it works the first time the player dies but not the second. Not sure what is happening here :confused:

1 Like

Oh nevermind! I changed the wait() to a wait(0.1) and it seems to be working. Thank you for your help!

1 Like

Are you sure? It works just fine for me.

1 Like

Alright great, happy to help. If you encounter any more issues, let me know.

2 Likes

Just wanted to add,

instead of a wait() or wait(.1) I would instead use RunService.Heartbeat:Wait()

wait()'s can be very unstable, and could produce different results every time. Meaning that there may be times the camera still does not face the right direction if it doesn’t wait long enough. wait()'s are also wasteful in resource as well since they can wait for longer than they need to. RunService should help it wait just as long as it needs to.

My example:

local RunService = game:GetService("RunService") -- new line adding RunService
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

localPlayer.CharacterAdded:Connect(function(char)
	RunService.Heartbeat:Wait() -- edited line
	local lookVec = char:WaitForChild("HumanoidRootPart").CFrame.LookVector
	Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Camera.CFrame.Position + lookVec)
end)

If heartbeat does not work then try renderstepped or stepped.

local Players = game:GetService("Players")

local RunService = game:GetService("RunService")

local Player = Players.LocalPlayer

local Character = Player.Character

local Camera = workspace.CurrentCamera

Camera.CameraType = Enum.CameraType.Scriptable

RunService.Stepped:Connect(function()

Camera.CFrame = Character:WaitForChild("HumanoidRootPart").CFrame

end)

I am going to explain a code a little bit. First of all, this is a LocalScript placed inside the StarterCharacterScripts. :yawning_face:

First we are going to define Players and RunService for updating the camera every single frame.

We are going to define the Player, Character and the Camera itself. :slightly_smiling_face:

We are going to make the CameraType of the Camera Scriptable, so we can actually work around with our Camera in a script right?

The next and the most important part of the code is to make our Camera’s CFrame to the same CFrame as the HumanoidRootPart. We are also going to update this line of code, by using RunService and the event called Stepped and connect that event to the function!!! :face_with_monocle:

Which means that anything you put inside of the function that is connected to the event called Stepped, it’s going to run depending on what that event DOES!
In our case this specific event is FIRING EVERY FRAME. This means that our function will run every single frame, because of this event. :exploding_head: :grinning:

Now what is it going to run? :thinking:

It’s going to set our Camera’s CFrame to the HumanoidRootPart’s CFrame. :face_with_raised_eyebrow:

If we didn’t use the Stepped event from the Run Service our Camera will be positioned only when player joins the game and it will stay at the same place forever! :open_mouth:

That is why built-in events are so important to us! :sunglasses:

I recommend checking out the links I’ve posted to gain more detailed information, because the more you know about this, your freedom with scripting is even more expanded!!! :face_with_monocle: :+1:

1 Like