I’m trying to write a script so that the camera looks at the other person.
However, instead of the camera looking at the other person, there’s a strange error where it’s looking at my character.
I couldn’t figure out the reason and why this error occurs no matter how much I researched, so I’m writing to you to find a solution to this script.
What I want is to write a script for the camera to look at the other person.
Script:
local Camera = game.Workspace.CurrentCamera
local Character = workspace.Rig
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CAMERA_DISTANCE = 7
local CAMERA_HEIGHT_OFFSET = 3
while true do
local characterPosition = HumanoidRootPart.Position
local cameraPosition = characterPosition - (HumanoidRootPart.CFrame.LookVector * CAMERA_DISTANCE) + Vector3.new(0, CAMERA_HEIGHT_OFFSET, 0)
Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(cameraPosition, characterPosition), 0.1)
wait(0.01)
end
This is the script I just wrote.
Please help me modify this script.
2 Likes
Try CFrame.lookAt() , if you don’t know how just look it up on google, it’s fairly simple.
2 Likes
Like what manipolatore
said, Use CFrame.lookAt()
. I’ve modified your script accordingly:
local Camera = game.Workspace.CurrentCamera
local Character = workspace.Rig
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
while wait(0.01) do
Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, HumanoidRootPart.Position)
end
2 Likes
The problem with your script was that it was taking the look vector of the humanoid that the camera should look at.
As other 2 people above said you should use CFrame.lookAt() to make it properly work.
I’ll also advice you to use RunService.RenderStepped instead of while loop to make the camera smoother. Put that into a variable so you could disable it in the future with :Dissconect() function.
Here’s the script that wrote to fix the problem:
local RunService=game["Run Service"]
local camera = game.Workspace.CurrentCamera
local lookAtCharacter = workspace.Rig
local lookAtRootpart = lookAtCharacter:WaitForChild("HumanoidRootPart")
local CameraChange:RBXScriptConnection
local CAMERA_HEIGHT_OFFSET = 3
CameraChange=RunService.RenderStepped:Connect(function()
local lookAt=CFrame.lookAt(camera.CFrame.Position,lookAtRootpart.Position+Vector3.new(0,CAMERA_HEIGHT_OFFSET,0))
camera.CFrame=camera.CFrame:Lerp(lookAt,0.1)
end)
1 Like
like the others said, but don’t forget camera type!
local Camera = workspace.CurrentCamera--consistency!
local Character = workspace.Rig
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CAMERA_DISTANCE = 7
local CAMERA_HEIGHT_OFFSET = 3
--most people forget to set camera type, if this is not set to scriptable
--the camera won't be modified.
Camera.CameraType = Enum.CameraType.Scriptable
--to reset the cam to the plr use:
--Camera.CameraType = Enum.CameraType.Custom
--this won't reset automatically when you die.
while true do
Camera.CFrame = CFrame.lookat(Camera.CFrame.Position, HumanoidRootPart.Position)
task.wait(0.1)
end
--I haven't tested this but I believe you can just use this instead of a loop:
--Camera.CameraSubject = Humanoid
--and then something like
--Camera.CameraType = Enum.CameraType.Orbit/Watch/Follow/etc...
hope this helps!
2 Likes
I’m sorry for the delayed response. While I’ve managed to make the camera focus on the opponent’s character, there’s something I couldn’t mention earlier. It’s about having the camera view the opponent’s character diagonally. Do you have a solution for this?
sample photo: 
1 Like