unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下
先上代码:
先是使用if进行判断的版本,支持实时锁定xyz的位置
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  | 
						using UnityEngine; public class FixedFollowCamera : MonoBehaviour { 	// 需要跟随的目标对象     public Transform target;     // 需要锁定的坐标(可以实时生效)     public bool freazeX, freazeY, freazeZ;     // 跟随的平滑时间(类似于滞后时间)     public float smoothTime = 0.3F;     private float xVelocity, yVelocity, zVelocity = 0.0F;     // 跟随的偏移量     private Vector3 offset; 	// 全局缓存的位置变量     private Vector3 oldPosition; 	// 记录初始位置     private Vector3 startPosition;     void Start()     {         startPosition = transform.position;         offset = transform.position - target.position;     }     void LateUpdate()     {         oldPosition = transform.position;         if (!freazeX)         {             oldPosition.x = Mathf.SmoothDamp(transform.position.x, target.position.x + offset.x, ref xVelocity, smoothTime);         }         if (!freazeY)         {             oldPosition.y = Mathf.SmoothDamp(transform.position.y, target.position.y + offset.y, ref yVelocity, smoothTime);         }         if (!freazeZ)         {             oldPosition.z = Mathf.SmoothDamp(transform.position.z, target.position.z + offset.z, ref zVelocity, smoothTime);         }         transform.position = oldPosition;     }     /// <summary>     /// 用于重新开始游戏时直接重置相机位置     /// </summary>     public void ResetPosition()     {         transform.position = startPosition;     } }  | 
					
delegate的版本
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79  | 
						using UnityEngine; // 更新位置委托,用于减少判断次数,具体性能升降未知 delegate void UpdatePosition(); public class FixedFollowCamera : MonoBehaviour {     // 需要跟随的目标对象     public Transform target;     // 需要锁定的坐标(无法实时生效)     public bool freazeX, freazeY, freazeZ;     // 跟随的平滑时间(类似于滞后时间)     public float smoothTime = 0.3F;     private float xVelocity, yVelocity, zVelocity = 0.0F;     // 跟随的偏移量     private Vector3 offset;     // 全局缓存的位置变量     private Vector3 oldPosition;     // 记录初始位置     private Vector3 startPosition;     private UpdatePosition JudgePosition;     void Start()     {         startPosition = transform.position;         offset = transform.position - target.position;         // 分配事件         if (!freazeX)         {             JudgePosition += MoveX;         }         if (!freazeY)         {             JudgePosition += MoveY;         }         if (!freazeZ)         {             JudgePosition += MoveZ;         }     }     void LateUpdate()     {         oldPosition = transform.position;         JudgePosition();         transform.position = oldPosition;     }     private void MoveX()     {         oldPosition.x = Mathf.SmoothDamp(transform.position.x, target.position.x + offset.x, ref xVelocity, smoothTime);     }     private void MoveY()     {         oldPosition.y = Mathf.SmoothDamp(transform.position.y, target.position.y + offset.y, ref yVelocity, smoothTime);     }     private void MoveZ()     {         oldPosition.z = Mathf.SmoothDamp(transform.position.z, target.position.z + offset.z, ref zVelocity, smoothTime);     }     /// <summary>     /// 用于重新开始游戏时直接重置相机位置     /// </summary>     public void ResetPosition()     {         transform.position = startPosition;     } }  | 
					
- 本文固定链接: http://www.u3d8.com/?p=597
 - 转载请注明: 网虫虫 在 u3d8.com 发表过
 
