so when the model is untouched the camera is able to look at the player. but when I rotate the camera 90 to 180 degrees the camera doesn’t track the player correctly when you look at it from the front and still tried to track the player as if it wasn’t rotated. is there a way to make it work regardless of the models rotation
sadly it’s not letting me provide a video so I’ll use screenshots:
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")
local tweenInfo = TweenInfo.new(.8,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)
local s = script.Parent
local mainCameraHead = s.MainCameraHead.MainCameraHead
local yAxis = s.Arm.SecondXaxis.YAxis
local xAxis = s.Arm.ArmContainer.XAxis
local LookAtPart
local function FindClosestPlayer()
for _, v in players:GetPlayers() do
local Character = v.Character
if Character then
local Head = Character:WaitForChild("Head")
if not LookAtPart then
LookAtPart = Head
else
if (mainCameraHead.Position - LookAtPart.Position).Magnitude > (mainCameraHead.Position - Head.Position).Magnitude then
LookAtPart = Head
end
end
end
end
end
while true do
if LookAtPart then
local CameraDirectionH = CFrame.new(mainCameraHead.Position,LookAtPart.Position).RightVector
local CameraDirectionV = CFrame.new(mainCameraHead.Position,LookAtPart.Position).UpVector * -1.5
local Goal = CFrame.new(-xAxis.C1.Y, _,_) * CFrame.Angles(0, math.asin(CameraDirectionH.z), 0)
local Goal2 = CFrame.new(_, -yAxis.C1.Y,_) * CFrame.Angles(math.asin(CameraDirectionV.z), 0, 0)
local Tween = tweenService:Create(xAxis, tweenInfo, {C1 = Goal}):Play()
if CameraDirectionV.Y < -1.2 then
local Tween2 = tweenService:Create(yAxis, tweenInfo, {C1 = Goal2}):Play()
--print("breaking.")
end
end
FindClosestPlayer()
task.wait()
end
local RunService = game:GetService("RunService")
local players = game:GetService("Players")
local CameraModel = script.Parent
local Base = CameraModel.Base -- the part the camera is connected to
local Camera = CameraModel.Camera -- the camera itself
local Motor6D = Base.Motor6D
local C0_Offset = Motor6D.C0
local LookAtPart
local function FindClosestPlayer()
for _, v in players:GetPlayers() do
local Character = v.Character
if Character then
local Head = Character:WaitForChild("Head")
if not LookAtPart then
LookAtPart = Head
else
if (Base.Position - LookAtPart.Position).Magnitude > (Base.Position - Head.Position).Magnitude then
LookAtPart = Head
end
end
end
end
end
RunService.Heartbeat:Connect(function(deltaTime)
if LookAtPart then
local camToPlayer = CFrame.new(Base.Position,LookAtPart.Position)
local baseRelativeCF = Base.CFrame:ToObjectSpace(camToPlayer)
Motor6D.C0 = Motor6D.C0:Lerp(CFrame.new(C0_Offset.Position)*baseRelativeCF*C0_Offset.Rotation,deltaTime*5)
end
FindClosestPlayer()
end)
Okay so first of all, you need to use RunService.Heartbeat if you want to use a loop that fires every frame because while loops have some delay.
Next, the basic code is as follows:
local camToPlayer = CFrame.new(Camera.Position,LookAtPart.Position)
local baseRelativeCF = Base.CFrame:ToObjectSpace(camToPlayer)
Motor6D.C0 = Motor6D.C0:Lerp(CFrame.new(C0_Offset.Position)*baseRelativeCF*C0_Offset.Rotation,deltaTime*5)
Where “Base” is the camera’s arm, “Camera” is the camera itself, “Motor6D” is the motor connecting the two together.
Now, on the first line I create a variable that just gets the look CFrame from the camera to the look part.
Next, I get the look CFrame relative to the base CFrame. This is the main thing that cancels out any and all rotation of the base itself. Because now it is relative to the base.
Next, I just applied the CFrame to the Motor. I use lerp here, which is far better than using tweens, which may cause issues like jittering. I also first add the C0 offset position and then the rotation, which helps to properly offset the Camera. Or else you may get unwanted results.