Unity 3D--摄像机平滑跟随(方法一)

[html]  view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class SmoothFollowerObj : MonoBehaviour {  
  5.   
  6.     private Vector3 targetPosition;  
  7.     private Vector3 position;  
  8.     private Vector3 velocity;  
  9.     private float smoothingTime;  
  10.     private float prediction;  
  11.   
  12.     public SmoothFollowerObj(float smoothingTime)  
  13.     {  
  14.         targetPosition = Vector3.zero;  
  15.         position = Vector3.zero;  
  16.         velocity = Vector3.zero;  
  17.         this.smoothingTime = smoothingTime;  
  18.         prediction = 1;  
  19.     }  
  20.   
  21.     public SmoothFollowerObj(float smoothingTime, float prediction)  
  22.     {  
  23.         targetPosition = Vector3.zero;  
  24.         position = Vector3.zero;  
  25.         velocity = Vector3.zero;  
  26.         this.smoothingTime = smoothingTime;  
  27.         this.prediction = prediction;  
  28.     }  
  29.   
  30.     // Update should be called once per frame  
  31.     public Vector3 Update(Vector3 targetPositionNew, float deltaTime)  
  32.     {  
  33.         Vector3 targetVelocity = (targetPositionNew - targetPosition) / deltaTime;  
  34.         targetPosition = targetPositionNew;  
  35.   
  36.         float d = Mathf.Min(1, deltaTime / smoothingTime);  
  37.         velocity = velocity * (1 - d) + (targetPosition + targetVelocity * prediction - position) * d;  
  38.   
  39.         position += velocity * Time.deltaTime;  
  40.         return position;  
  41.     }  
  42.   
  43.     public Vector3 Update(Vector3 targetPositionNew, float deltaTime, bool reset)  
  44.     {  
  45.         if (reset)  
  46.         {  
  47.             targetPosition = targetPositionNew;  
  48.             position = targetPositionNew;  
  49.             velocity = Vector3.zero;  
  50.             return position;  
  51.         }  
  52.         return Update(targetPositionNew, deltaTime);  
  53.     }  
  54.   
  55.     public Vector3 GetPosition() { return position; }  
  56.     public Vector3 GetVelocity() { return velocity; }  
  57. }  


下面该段脚本赋给摄像机,被跟随物体赋给character即可;

[html]  view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NoGravityCamera : MonoBehaviour {  
  5.   
  6.     public GameObject character;  
  7.     public Vector3 positionVector;  
  8.     public Vector3 lookVector;  
  9.     private SmoothFollowerObj posFollow;  
  10.     private SmoothFollowerObj lookFollow;  
  11.     private Vector3 lastVelocityDir;  
  12.     private Vector3 lastPos;  
  13.   
  14.     // Use this for initialization  
  15.     void Start()  
  16.     {  
  17.         //positionVector = new Vector3(0, 2, 4);  
  18.         //lookVector = new Vector3(0, 0, 1.5f);  
  19.         posFollow = new SmoothFollowerObj(0.5f, 0.5f);  
  20.         lookFollow = new SmoothFollowerObj(0.1f, 0.0f);  
  21.         posFollow.Update(transform.position, 0, true);  
  22.         lookFollow.Update(character.transform.position, 0, true);  
  23.         lastVelocityDir = character.transform.forward;  
  24.         lastPos = character.transform.position;  
  25.     }  
  26.   
  27.     // Update is called once per frame  
  28.     void LateUpdate()  
  29.     {  
  30.         lastVelocityDir += (character.transform.position - lastPos) * 8;  
  31.         lastPos = character.transform.position;  
  32.         lastVelocityDir += character.transform.forward * Time.deltaTime;  
  33.         lastVelocityDir = lastVelocityDir.normalized;  
  34.         Vector3 horizontal = transform.position - character.transform.position;  
  35.         Vector3 horizontal2 = horizontal;  
  36.         Vector3 vertical = character.transform.up;  
  37.         Vector3.OrthoNormalize(ref vertical, ref horizontal2);  
  38.         if (horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;  
  39.         transform.position = posFollow.Update(  
  40.             character.transform.position + horizontal * Mathf.Abs(positionVector.z) + vertical * positionVector.y,  
  41.             Time.deltaTime  
  42.         );  
  43.   
  44.         horizontal = lastVelocityDir;  
  45.         Vector3 look = lookFollow.Update(character.transform.position + horizontal * lookVector.z - vertical * lookVector.y, Time.deltaTime);  
  46.         transform.rotation = Quaternion.FromToRotation(transform.forward, look - transform.position) * transform.rotation;  
  47.     }  
  48. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值