Advertisement
maycool1233

aaaaa

May 6th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public class CameraControl : MonoBehaviour
  7. {
  8.     public float m_DampTime = 0.2f;                 // Approximate time for the camera to refocus.
  9.     public float m_ScreenEdgeBuffer = 4f;           // Space between the top/bottom most target and the screen edge.
  10.     public float m_MinSize = 6.5f;                  // The smallest orthographic size the camera can be.
  11.     [HideInInspector]
  12.     public Transform[] m_Targets; // All the targets the camera needs to encompass.
  13.  
  14.  
  15.     private Camera m_Camera;                        // Used for referencing the camera.
  16.     private float m_ZoomSpeed;                      // Reference speed for the smooth damping of the orthographic size.
  17.     private Vector3 m_MoveVelocity;                 // Reference velocity for the smooth damping of the position.
  18.     private Vector3 m_DesiredPosition;              // The position the camera is moving towards.
  19.  
  20.  
  21.     private void Awake()
  22.     {
  23.         m_Camera = GetComponentInChildren<Camera>();
  24.     }
  25.  
  26.     void Update()
  27.     { //paste it in your Update method
  28.  
  29.         Input.GetKey(KeyCode.Z);
  30.         {
  31.  
  32.             StartCoroutine(WaitThenCallThisConstantly(0.0005f, 0.0075f, 0, 20)) ;
  33.         }
  34.     }
  35.  
  36.     private void StartCoroutine(IEnumerable<IEnumerator> enumerable)
  37.     {
  38.         throw new NotImplementedException();
  39.     }
  40.  
  41.     System.Collections.Generic.IEnumerable<IEnumerator> WaitThenCallThisConstantly(float time, float smoothTime, int counter, int maxTimes)
  42.     {
  43.  
  44.         yield return new WaitForSeconds(time);
  45.  
  46.         float zoom = 4.25f; float smooth = 5.0f;
  47.         float normal = 4.95f; //increase this to a bigger value to zoom out even more
  48.  
  49.  
  50.         counter++;
  51.         if (counter < maxTimes)
  52.         {
  53.  
  54.             m_Camera.orthographicSize = Mathf.Lerp(m_Camera.orthographicSize, normal, Time.deltaTime * (counter / 2.0f) * smooth);
  55.             StartCoroutine(WaitThenCallThisConstantly(time, smoothTime, counter, maxTimes));
  56.  
  57.         }
  58.     }
  59.  
  60.     private void Move()
  61.     {
  62.         // Find the average position of the targets.
  63.         FindAveragePosition();
  64.  
  65.         // Smoothly transition to that position.
  66.         transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
  67.     }
  68.  
  69.  
  70.     private void FindAveragePosition()
  71.     {
  72.         Vector3 averagePos = new Vector3();
  73.         int numTargets = 0;
  74.  
  75.         // Go through all the targets and add their positions together.
  76.         for (int i = 0; i < m_Targets.Length; i++)
  77.         {
  78.             // If the target isn't active, go on to the next one.
  79.             if (!m_Targets[i].gameObject.activeSelf)
  80.                 continue;
  81.  
  82.             // Add to the average and increment the number of targets in the average.
  83.             averagePos += m_Targets[i].position;
  84.             numTargets++;
  85.         }
  86.  
  87.         // If there are targets divide the sum of the positions by the number of them to find the average.
  88.         if (numTargets > 0)
  89.             averagePos /= numTargets;
  90.  
  91.         // Keep the same y value.
  92.         averagePos.y = transform.position.y;
  93.  
  94.         // The desired position is the average position;
  95.         m_DesiredPosition = averagePos;
  96.     }
  97.  
  98.  
  99.     private void Zoom()
  100.     {
  101.         // Find the required size based on the desired position and smoothly transition to that size.
  102.         float requiredSize = FindRequiredSize();
  103.         m_Camera.orthographicSize = Mathf.SmoothDamp(m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
  104.     }
  105.  
  106.  
  107.     private float FindRequiredSize()
  108.     {
  109.         // Find the position the camera rig is moving towards in its local space.
  110.         Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
  111.  
  112.         // Start the camera's size calculation at zero.
  113.         float size = 0f;
  114.  
  115.         // Go through all the targets...
  116.         for (int i = 0; i < m_Targets.Length; i++)
  117.         {
  118.             // ... and if they aren't active continue on to the next target.
  119.             if (!m_Targets[i].gameObject.activeSelf)
  120.                 continue;
  121.  
  122.             // Otherwise, find the position of the target in the camera's local space.
  123.             Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
  124.  
  125.             // Find the position of the target from the desired position of the camera's local space.
  126.             Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
  127.  
  128.             // Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
  129.             size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));
  130.  
  131.             // Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
  132.             size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
  133.         }
  134.  
  135.         // Add the edge buffer to the size.
  136.         size += m_ScreenEdgeBuffer;
  137.  
  138.         // Make sure the camera's size isn't below the minimum.
  139.         size = Mathf.Max(size, m_MinSize);
  140.  
  141.         return size;
  142.     }
  143.  
  144.  
  145.     public void SetStartPositionAndSize()
  146.     {
  147.         // Find the desired position.
  148.         FindAveragePosition();
  149.  
  150.         // Set the camera's position to the desired position without damping.
  151.         transform.position = m_DesiredPosition;
  152.  
  153.         // Find and set the required size of the camera.
  154.         m_Camera.orthographicSize = FindRequiredSize();
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement