Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class smoothFollow : MonoBehaviour {
  7.  
  8.  
  9.     public Transform target;
  10.  
  11.     public float zoomDamp = 2.0f;
  12.     public float movementDamp = 2.0f;
  13.     public TileSystem tilesystem;
  14.     public float distance = 20.0f;
  15.     // Use this for initialization
  16.     void Start () {
  17.  
  18.     }
  19.    
  20.     // Update is called once per frame
  21.     void FixedUpdate () {
  22.         float fWidth = tilesystem.RowCount * (tilesystem.CellSize.x + tilesystem.CellSize.y);
  23.         float fT = fWidth / Screen.width * Screen.height;
  24.         fT = fT / (2.0f * Mathf.Tan (0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad));
  25.         float currentZoom = transform.position.z;
  26.         Vector3 wantedPosition = target.position;
  27.         Vector3 currentPosition = transform.position;
  28.  
  29.         GameObject[] GameObjects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
  30.         List<GameObject> countableGameobjects = new List<GameObject>();
  31.         foreach (GameObject thisGameObject in GameObjects)
  32.         {
  33.             if (Vector3.Distance(thisGameObject.transform.position, target.transform.position) <= distance)
  34.             {
  35.                 countableGameobjects.Add(thisGameObject);
  36.             }
  37.             else if (Vector3.Distance(thisGameObject.transform.position, target.transform.position) >= distance)
  38.             {
  39.                 countableGameobjects.Remove(thisGameObject);
  40.             }
  41.            
  42.         }
  43.         float minHeight = float.MaxValue;
  44.         float maxHeight = float.MinValue;
  45.         foreach(GameObject thisGameObjectLow in countableGameobjects){
  46.             if (thisGameObjectLow.transform.position.y < minHeight)
  47.             {
  48.                 minHeight = thisGameObjectLow.transform.position.y;
  49.             }
  50.             if (thisGameObjectLow.transform.position.y > maxHeight)
  51.             {
  52.                 maxHeight = thisGameObjectLow.transform.position.y;
  53.             }
  54.         }
  55.         float wantedCameraZoom = ((Mathf.Abs(maxHeight) + Mathf.Abs(minHeight)) ) * -1;
  56.      
  57.         currentZoom = Mathf.Lerp (currentZoom, wantedCameraZoom, zoomDamp);
  58.         currentPosition.x = Mathf.Lerp (currentPosition.x, wantedPosition.x, movementDamp);
  59.         currentPosition.y = Mathf.Lerp (currentPosition.y, wantedPosition.y, movementDamp);
  60.         transform.position = new Vector3(currentPosition.x, currentPosition.y,currentZoom);
  61.        
  62.     }
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement