Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get the distance of the Camera from the player's character?

Asked by
Cjjdawg -9
5 years ago

Hi I would really appreciate anyone who can help me solve my problem I writing animation camera script to zoom up the camera to a part when the player touches it. I would really like to know how can I get the current z distance of the camera from the user. Here is my code so far:

function module.PanAndZoomInCamera(PLAYERMOVER)

local Camera = workspace.CurrentCamera--Current camera

--Auxillary Variables for zooming out

local TargetDist = 0

local CurrentDist = Camera.CFrame.Z--This is where i am trying to get the camera pos, and it does not worl

local CurrentRot = -90

--Camera properties

Camera.CameraType = Enum.CameraType.Scriptable

Camera.CameraSubject = PLAYERMOVER

Camera.CFrame = PLAYERMOVER.PrimaryPart.CFrame:ToWorldSpace(CFrame.Angles(0,math.rad(-90),0))*CFrame.new(0,0,15)

while CurrentDist > TargetDist do

Camera.CFrame = PLAYERMOVER.PrimaryPart.CFrame:ToWorldSpace(CFrame.Angles(0,math.rad(CurrentRot),0))*CFrame.new(0,0,CurrentDist)

CurrentDist = CurrentDist - (15/9)

wait(0.001)

end

end

Thank you in advance for any help :)

0
By Z distance do you mean the distance/depth of the camera to the Character/PrimaryPart? You can use (Camera.CFrame.p - PrimaryPart.Position).magnitude to do so. Rheines 661 — 5y
0
or be modern and use (Camera.CFrame.Position - PrimaryPart.Position).Magnitude User#24403 69 — 5y

Answer this question