Search Unity

MULTIPLE TARGET CAMERA

Discussion in '2D' started by wowcrazyguy, Aug 19, 2019.

  1. wowcrazyguy

    wowcrazyguy

    Joined:
    Sep 24, 2018
    Posts:
    49
    Dear all,

    I follow this video (
    ) trying to get my single camera to follow 4 objects but didn't succeed. The camera indeed sets the middle point between the 4 objects, but it cannot enlarge or widen the camera size to cover them, not even two objects as I have tried. I try to have something like this game (https://www.gamasutra.com/blogs/Nic...492/Single_Camera_System_for_Four_Players.php). It gives concept but did not show how he achieves it.

    Below is the code I got from the youtube link. Thank you

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Camera))]
    6. public class multiTargetCamera : MonoBehaviour
    7. {
    8.     public List<Transform> targets;
    9.  
    10.     public Vector3 offset;
    11.     public float smoothTime = .5f;
    12.  
    13.     public float minZoom = 40f;
    14.     public float maxZoom = 10f;
    15.     public float zoomLimiter = 50f;
    16.  
    17.     private Vector3 velocity;
    18.     private Camera cam;
    19.  
    20.  
    21.     private void Start()
    22.     {
    23.  
    24.         cam = GetComponent<Camera>();
    25.  
    26.     }
    27.  
    28.     private void LateUpdate()
    29.     {
    30.  
    31.         if (targets.Count == 0)
    32.             return;
    33.  
    34.  
    35.         move();
    36.         zoom();
    37.  
    38.     }
    39.  
    40.     void zoom()
    41.     {
    42.  
    43.         float newZoom = Mathf.Lerp(maxZoom, minZoom, getGreatestDistance() / zoomLimiter);
    44.         cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, newZoom, Time.deltaTime);
    45.  
    46.  
    47.     }
    48.  
    49.  
    50.     void move()
    51.     {
    52.  
    53.         Vector3 centerPoint = getCenterPoint();
    54.         Vector3 newPosition = centerPoint + offset;
    55.         transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
    56.  
    57.     }
    58.  
    59.     float getGreatestDistance()
    60.     {
    61.  
    62.         var bounds = new Bounds(targets[0].position, Vector3.zero);
    63.         for (int i = 0; i < targets.Count; i++)
    64.         {
    65.  
    66.             bounds.Encapsulate(targets[i].position);
    67.  
    68.  
    69.         }
    70.  
    71.         return bounds.size.x;
    72.  
    73.     }
    74.  
    75.  
    76.     Vector3 getCenterPoint()
    77.     {
    78.  
    79.         if (targets.Count == 1)
    80.         {
    81.  
    82.             return targets[0].position;
    83.  
    84.  
    85.         }
    86.  
    87.         var bounds = new Bounds(targets[0].position, Vector3.zero);
    88.         for (int i = 0; i < targets.Count; i++)
    89.         {
    90.  
    91.  
    92.             bounds.Encapsulate(targets[i].position);
    93.  
    94.         }
    95.  
    96.         return bounds.center;
    97.  
    98.  
    99.     }
    100.  
    101.  
    102. }
    103.  
     
  2. adaberk50

    adaberk50

    Joined:
    Mar 22, 2020
    Posts:
    1
    I know it' a bit old but, try to decrease Zoom Limiter to something like ten or twenty.