What do you want to achieve?
When the player spawns I want their camera to face the same direction as their HumanoidRootPart is facing.
What is the issue?
No matter what I try, I cannot get the camera to rotate at all.
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)
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?
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.
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
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
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?
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)
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.
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.
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!!!
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.
Now what is it going to run?
It’s going to set our Camera’s CFrame to the HumanoidRootPart’s CFrame.
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!
That is why built-in events are so important to us!
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!!!