zooming out camera as two players move further apart in 2D game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dearme

I would like to make it so that the default scale/zoom of the camera is 1, but the further away are 2 players - the more camer zooms out (with a limit set to 0.1 as an example)

I have got this functionality working to some extent, but the problem is that it MOSTLY works for one of the axis (in my case X), If the player moves further away on the Y axis, then the zoom out is occurring later after the player has already moved outside of the visible area!

I would like the zoom-out functionality to start scaling the camera soon after one of the players are within 10% margin of the viewport boundaries. In other words, when the distance between the 2 players is more than 80& of the camera’s visible area in any direction (if that makes sense?)

I am using Godot 4 and a C# version of it. Here is the camera script I am using right now:

using Godot;
using System;
using System.Collections.Generic;

public partial class Camera : Camera2D
{
	private List<Node2D> players = new List<Node2D>();

	private float maxRelativeToScreenRatio = 0.8f;
	private float minScale = 0.1f;
	private float maxScale = 1.0f;
	private float scaleSpeed = 10.0f;

	public override void _Ready()
	{
		// Find all nodes with the "Player" group
		foreach (Node2D player in GetTree().GetNodesInGroup("player"))
		{
			players.Add(player);
		}
	}

	public override void _Process(double delta)
	{
		if (players.Count == 0)
		{
			return;
		}

		// Calculate the average position of all players
		Vector2 averagePosition = Vector2.Zero;
		foreach (Node2D player in players)
		{
			averagePosition += player.GlobalPosition;
		}
		averagePosition /= players.Count;

		// Calculate the distance between the two furthest players
		float maxDistance = float.MinValue;
		for (int i = 0; i < players.Count - 1; i++)
		{
			for (int j = i + 1; j < players.Count; j++)
			{
				float distance = players[i].GlobalPosition.DistanceTo(players[j].GlobalPosition);
				if (distance > maxDistance)
				{
					maxDistance = distance;
				}
			}
		}

		var viewport = this.GetViewportRect();
		var viewportSize = viewport.Size;

		var currentScale = this.Zoom.X;

		// check the current distance relative to viewport size
		var distanceToViewportSizeProportion = (maxDistance / viewportSize.Length()) * currentScale;

		// calculate the new scale
		var newScale = currentScale - (currentScale * (distanceToViewportSizeProportion - maxRelativeToScreenRatio));

		// make sure that zoom is within limits
		newScale = Mathf.Clamp(newScale, minScale, maxScale);

		// Zoom in-out slowly
		newScale = Mathf.Lerp(currentScale, newScale, (float)delta * scaleSpeed);

		// Set the new scale
		Zoom = new Vector2(newScale, newScale);

		// Set the camera position to the average position
		GlobalPosition = averagePosition;
	}
}

I know this is weak excuse but I just started learning godot today with no prior game-dev knowledge so the concept of vectors and how they behave in 2d space is still new to me. I would appreciate to get some help on the matter.

p.s. one of the other thing that script does is sets the position of the camera to the middle point between the players. This part works fine.

Thanks