0

Here is the script that I am using for the camera. Right now, I manually have to set the boundaries by positioning the camera where it looks like it's the limit of the border. There are a few problems though. If i change the aspect ratio of the viewport in Unity, the boundaries won't work as intended.

What I'm looking for is a way to - see the maximum boundaries set(like an empty game objectt idk) - see where the camera can move(in the inspector with Gizmos.DrawLine)

Basically i want a system that i can reuse for other scenes with different boundaries size and that adapts to the screen's size. Thanks in advance

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

public class FollowTarget : MonoBehaviour
{
    public GameObject target;
    public Vector3 offset;
    public float cameraSpeed;
    private Vector3 velocity = Vector3.zero;
    private Vector3 targetPosition;

    public float leftLimit;
    public float rightLimit;
    public float topLimit;
    public float botLimit;

    // Start is called before the first frame update
    void Start()
    {
        targetPosition = target.transform.position - offset;
    }

    void FixedUpdate() {
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, cameraSpeed);
    }

    // Update is called once per frame
    void Update()
    {
        float clampedX = Mathf.Clamp(transform.position.x, leftLimit, rightLimit);
        float clampedY = Mathf.Clamp(transform.position.y, topLimit, botLimit);
        targetPosition = target.transform.position - offset;
    }
}
Alex Ivan
  • 1
  • 1
  • 2
  • tell me if this helps: https://stackoverflow.com/questions/60001501/maximize-zoom-distance-to-fit-2-objects – Lotan May 20 '20 at 09:15
  • hm not quite. what i just want is to have like a box defined for example. and the camera should only be able to move within that box. exactly like you would do with a collider. but u can't use colliders on camera can you? – Alex Ivan May 20 '20 at 09:33
  • What about using Cinemachine? https://www.youtube.com/watch?v=mWqX8GxeCBk – Lotan May 20 '20 at 09:34
  • 1
    Hm a bit too much tho for my game. But nvm i found some ideas , put them together now it works. Here's the code – Alex Ivan May 20 '20 at 11:46
  • 1
    the biggest problem was that it wasnt a reliable thing to use in multiple scenes, and it wouldn't adapt to different aspect ratios. Thanx y'all for help ! – Alex Ivan May 20 '20 at 11:48

1 Answers1

0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    private Camera cam;
    private BoxCollider2D cameraBox;
    public BoxCollider2D boundary;
    public Transform target;

    private float leftPivot;
    private float rightPivot;
    private float topPivot;
    private float botPivot;

    void Start(){
        cam = gameObject.GetComponent<Camera>();
        cameraBox = cam.GetComponent<BoxCollider2D>();
    }
    void Update(){
        AspectRatioBoxChange();
        CalculateCameraPivot();
        FollowPlayer();
    }

    void AspectRatioBoxChange(){
        float height = 2f * cam.orthographicSize;
        float width = height * cam.aspect;
        cameraBox.size = new Vector2(width, height);
    }

    void CalculateCameraPivot(){
        botPivot = boundary.bounds.min.y + cameraBox.size.y/2;
        topPivot = boundary.bounds.max.y - cameraBox.size.y/2;
        leftPivot = boundary.bounds.min.x + cameraBox.size.x/2;
        rightPivot = boundary.bounds.max.x - cameraBox.size.x/2;

    }

    void FollowPlayer(){
        transform.position = new Vector3(Mathf.Clamp(target.position.x, leftPivot, rightPivot),
                                         Mathf.Clamp(target.position.y, botPivot, topPivot),
                                         transform.position.z);
    }

}
Alex Ivan
  • 1
  • 1
  • 2