In my game, a player can press a button to activate a top-down view of the map. They can press the button again to recover their Custom
camera.
The problem I’m having is that the camera goes into a first-person view for a split second, and then the camera fixes itself after I change the CameraType
to Custom
. I want the camera to smoothly fix itself; not abruptly.
Here’s what’s happening:
Here’s my code:
local TOP_DOWN_OFFSET = Vector3.new(-1, 90, 0);
local function ToggleCamera(actionName, inputState)
if CameraMode == "TD" then
-- Classic
RunService:UnbindFromRenderStep("TopDownCamera");
Camera.CameraSubject = Player.Character.Humanoid;
Camera.CameraType = Enum.CameraType.Custom;
local Tween = TweenService:Create(Camera, TweenInfo.new(0.4), {CFrame = Player.Character.Head.CFrame});
Tween:Play();
else
-- Top-down
Camera.CameraType = Enum.CameraType.Scriptable;
local CurrentTopDown = Vector3.new(-1, 0, 0);
local function UpdateCamera()
if CurrentTopDown ~= TOP_DOWN_OFFSET then
CurrentTopDown = Vector3.new(-1, CurrentTopDown.Y + 5, 0);
RunService.Heartbeat:Wait();
end;
if Player.Character then
local PlayerPosition = Player.Character.HumanoidRootPart.Position;
local CameraPosition = PlayerPosition + CurrentTopDown;
-- Follow the player
Camera.CoordinateFrame = CFrame.new(CameraPosition, PlayerPosition);
end;
end;
RunService:BindToRenderStep("TopDownCamera", Enum.RenderPriority.Camera.Value, UpdateCamera);
CameraMode = "TD";
end;
end;
ContextActionService:BindAction("ToggleCamera", ToggleCamera, false, Enum.KeyCode.C);
What am I doing wrong?