2D游戏相机跟随

做2D游戏的时候,无非有三种模式,一种是只有竖直向上,一种是只有水平方向,一种是有水平又有竖直方向,我最近做游戏多关卡模式,不同模式就有着不一样的相机控制,按照平时的写法,也许有很多人就一下子写了三个相机脚本,现在我用了一个枚举类型来控制三个不一样的相机,贴代码:
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4.   
  5. public class CameraCtrl : MonoBehaviour {  
  6.   
  7.     public enum CameraType  
  8.     {  
  9.         Vertical,  
  10.         Horizontal,  
  11.         Normal  
  12.     }  
  13.   
  14.     public CameraType cameraType;  
  15.     public float dampTime = 1.5f;  
  16.     public Transform target;  
  17.     // 相机移动速度,初始速度清零  
  18.     private Vector3 velocity = Vector3.zero;  
  19.   
  20.     // 相机单例  
  21.     private static CameraCtrl instance;  
  22.     public static CameraCtrl Instance {  
  23.         get {  
  24.             return instance;  
  25.         }  
  26.     }  
  27.   
  28.     // 屏幕的默认宽高的1/100 (预编译)  
  29. #if UNITY_ANDROID  
  30.     private static float devHeight = 8.54f;  
  31.     private static float devWidth = 4.8f;  
  32. #elif UNITY_IPHONE  
  33.     private static float devHeight = 9.6f;  
  34.     private static float devWidth = 6.4f;  
  35. #else  
  36.     private static float devHeight = 19.20f;  
  37.     private static float devWidth = 10.80f;  
  38. #endif  
  39.   
  40.     // Use this for initialization  
  41.     void Awake () {  
  42.         instance = this;  
  43.         // 屏幕适配  
  44.         float screenHeight = Screen.height;  
  45.           
  46.         //Debug.Log ("screenHeight = " + screenHeight);  
  47.           
  48.         //this.GetComponent<Camera>().orthographicSize = screenHeight / 200.0f;  
  49.           
  50.         float orthographicSize = this.GetComponent<Camera>().orthographicSize;  
  51.           
  52.         float aspectRatio = Screen.width * 1.0f / Screen.height;  
  53.           
  54.         float cameraWidth = orthographicSize * 2 * aspectRatio;  
  55.           
  56.         //Debug.Log ("cameraWidth = " + cameraWidth);  
  57.           
  58.         if (cameraWidth < devWidth)  
  59.         {  
  60.             orthographicSize = devWidth / (2 * aspectRatio);  
  61.             Debug.Log ("new orthographicSize = " + orthographicSize);  
  62.             this.GetComponent<Camera>().orthographicSize = orthographicSize;  
  63.         }  
  64.           
  65.     }  
  66.   
  67.     // Update is called once per frame  
  68.     void LateUpdate ()   
  69.     {  
  70.         if (target) {  
  71.             SetCamera();  
  72.         } else {  
  73.             SetTarget();  
  74.         }  
  75.     }  
  76.   
  77.     // 设置相机  
  78.     void SetCamera () {  
  79.         Vector3 point = GetComponent<Camera> ().WorldToViewportPoint (target.position);  
  80.         Vector3 delta = target.position - GetComponent<Camera> ().ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, point.z));   
  81.         Vector3 destination = transform.position + delta;  
  82.         switch (cameraType) {  
  83.         case CameraType.Vertical:// 竖直相机  
  84.             transform.position = Vector3.SmoothDamp (transform.position,   
  85.                                                      new Vector3 (transform.position.x, destination.y, destination.z),   
  86.                                                      ref velocity, dampTime);  
  87.             break;  
  88.         case CameraType.Horizontal:// 水平相机  
  89.             transform.position = Vector3.SmoothDamp (transform.position,   
  90.                                                      new Vector3 (destination.x, transform.position.y, destination.z),   
  91.                                                      ref velocity, dampTime);  
  92.             break;  
  93.         case CameraType.Normal:// 无限制的相机  
  94.             transform.position = Vector3.SmoothDamp (transform.position,   
  95.                                                      destination,   
  96.                                                      ref velocity, dampTime);  
  97.             break;  
  98.         default:  
  99.             break;  
  100.         }  
  101.     }  
  102.     // 设置目标  
  103.     void SetTarget () {  
  104.         target = GameObject.FindGameObjectWithTag ("Player").transform;  
  105.     }  
  106. }  

猜你喜欢

转载自blog.csdn.net/qq_38112703/article/details/79855849