Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- public class smoothFollow : MonoBehaviour {
- public Transform target;
- public float zoomDamp = 2.0f;
- public float movementDamp = 2.0f;
- public TileSystem tilesystem;
- public float distance = 20.0f;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void FixedUpdate () {
- float fWidth = tilesystem.RowCount * (tilesystem.CellSize.x + tilesystem.CellSize.y);
- float fT = fWidth / Screen.width * Screen.height;
- fT = fT / (2.0f * Mathf.Tan (0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad));
- float currentZoom = transform.position.z;
- Vector3 wantedPosition = target.position;
- Vector3 currentPosition = transform.position;
- GameObject[] GameObjects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
- List<GameObject> countableGameobjects = new List<GameObject>();
- foreach (GameObject thisGameObject in GameObjects)
- {
- if (Vector3.Distance(thisGameObject.transform.position, target.transform.position) <= distance)
- {
- countableGameobjects.Add(thisGameObject);
- }
- else if (Vector3.Distance(thisGameObject.transform.position, target.transform.position) >= distance)
- {
- countableGameobjects.Remove(thisGameObject);
- }
- }
- float minHeight = float.MaxValue;
- float maxHeight = float.MinValue;
- foreach(GameObject thisGameObjectLow in countableGameobjects){
- if (thisGameObjectLow.transform.position.y < minHeight)
- {
- minHeight = thisGameObjectLow.transform.position.y;
- }
- if (thisGameObjectLow.transform.position.y > maxHeight)
- {
- maxHeight = thisGameObjectLow.transform.position.y;
- }
- }
- float wantedCameraZoom = ((Mathf.Abs(maxHeight) + Mathf.Abs(minHeight)) ) * -1;
- currentZoom = Mathf.Lerp (currentZoom, wantedCameraZoom, zoomDamp);
- currentPosition.x = Mathf.Lerp (currentPosition.x, wantedPosition.x, movementDamp);
- currentPosition.y = Mathf.Lerp (currentPosition.y, wantedPosition.y, movementDamp);
- transform.position = new Vector3(currentPosition.x, currentPosition.y,currentZoom);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement