#My Custom Camera issues

1 messages · Page 1 of 1 (latest)

ancient tusk
#

My custom camera is prioritising one object and zooming in very far disregarding my variable. How do I fix it?

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class CustomCameraFollow : MonoBehaviour
{
[Header("Camera Settings")]
[SerializeField] private float cameraLerpSped;
[SerializeField] private float zoomLerpSpeed;
[SerializeField] private float desiredDistanceFromPlayers;

[Header("References")]
[SerializeField] private Transform cameraTransform;
public List<Transform> followObjects = new List<Transform>();

private Vector3 followCenter;
private float xRange;
private float yRange;

void Update()
{
    CalculateXandYRanges();
    CalculatePositionAvrage();
    FollowPoint();
    AdjustZoom();
}

private void CalculateXandYRanges()
{
    float xMax = float.MinValue;
    float xMin = float.MaxValue;
    float yMax = float.MinValue;
    float yMin = float.MaxValue;

    foreach (var obj in followObjects)
    {
        if (obj.position.x > xMax) xMax = obj.position.x;
        if (obj.position.x < xMin) xMin = obj.position.x;
        if (obj.position.y > yMax) yMax = obj.position.y;
        if (obj.position.y < yMin) yMin = obj.position.y;
    }

    xRange = Mathf.Abs(xMax - xMin);
    yRange = Mathf.Abs(yMax - yMin);
}

private void CalculatePositionAvrage()
{
    Vector3 avragePosition = new Vector3(0, 0, 0);
    foreach (var obj in followObjects)
    {
        avragePosition += obj.position;
    }
    followCenter = avragePosition / followObjects.Count;
}
#

private void FollowPoint()
{
Vector3 targetCameraPosition = new Vector3(followCenter.x, followCenter.y, -10);
cameraTransform.position = Vector3.Lerp(cameraTransform.position, targetCameraPosition, cameraLerpSped * Time.deltaTime);
}

private void AdjustZoom()
{
    float targetOrthographicSize = Mathf.Max(xRange, yRange) * desiredDistanceFromPlayers;
    Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, targetOrthographicSize, zoomLerpSpeed * Time.deltaTime);
}

}

#

seccond part

copper terrace
#

Your zoom is blowing out because it takes the furthest object into account. I can help fix it by clamping the zoom and averaging better so outliers don’t mess it up. Do you want the cam to always fit all objects, or just stay closer to the group center?
@ancient tusk

ancient tusk