Unity摄像机跟随Target快速位移和旋转

来源:互联网 发布:网络存储空间 编辑:程序博客网 时间:2024/06/06 14:24
[html] view plaincopyprint?
  1. public Transform target;  
  2.     public float distance ;  
  3.     public float targetHeight;  
  4.     public float PitchAngle;  
  5.     private float x = 0.0f;  
  6.     private float y = 0.0f;    
  7.        
  8.     void Start () {  
  9.         var angles = transform.eulerAngles;  
  10.         x = angles.x;  
  11.         y = angles.y;  
  12.     }  
  13.        
  14.     void LateUpdate ()   
  15.     {  
  16.        if(!target)  
  17.           return;  
  18.          
  19.        y = target.eulerAngles.y;  
  20.   
  21.        // ROTATE CAMERA:  
  22.        Quaternion rotation = Quaternion.Euler(x-PitchAngle, y, 0);  
  23.        transform.rotation = rotation;  
  24.          
  25.        // POSITION CAMERA:  
  26.        Vector3 position = target.position - (rotation * Vector3.forward * distance + new Vector3(0,-targetHeight,0));  
  27.        transform.position = position;  
  28.     }  
0 0
原创粉丝点击