1 using UnityEngine;
  2 using System.Collections;
  3 [AddComponentMenu("Camera-Control/3dsMax Camera Style")]
  4 public class MaxCamera : MonoBehaviour
  5 {
  6     public Transform target;
  7     public Vector3 targetOffset;
  8     public float distance = 5.0f;
  9     public float maxDistance = 20;
 10     public float minDistance = .6f;
 11     public float xSpeed = 200.0f;
 12     public float ySpeed = 200.0f;
 13     public int yMinLimit = -80;
 14     public int yMaxLimit = 80;
 15     public int zoomRate = 40;
 16     public float panSpeed = 0.3f;
 17     public float zoomDampening = 5.0f;
 18     private float xDeg = 0.0f;
 19     private float yDeg = 0.0f;
 20     private float currentDistance;
 21     private float desiredDistance;
 22     private Quaternion currentRotation;
 23     private Quaternion desiredRotation;
 24     private Quaternion rotation;
 25     private Vector3 position;
 26     //bool isshang;
 27     //bool isxia;
 28     //bool iszuo;
 29     //bool isyou;
 30     //public bool ispingyi;
 31 
 32     void Start() { Init(); }
 33     void OnEnable() { Init(); }
 34     public void Init()
 35     {
 36         //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
 37         if (!target)
 38         {
 39             GameObject go = new GameObject("Cam Target");
 40             go.transform.position = transform.position + (transform.forward * distance);
 41             target = go.transform;
 42         }
 43         distance = Vector3.Distance(transform.position, target.position);
 44         currentDistance = distance;
 45         desiredDistance = distance;
 46                 
 47         //be sure to grab the current rotations as starting points.
 48         position = transform.position;
 49         rotation = transform.rotation;
 50         currentRotation = transform.rotation;
 51         desiredRotation = transform.rotation;
 52         
 53         xDeg = Vector3.Angle(Vector3.right, transform.right );
 54         yDeg = Vector3.Angle(Vector3.up, transform.up );
 55     }
 56     
 57     void LateUpdate()
 58     {
 59         // If Control and Alt and Middle button? ZOOM!
 60         if (Input.GetMouseButton(2)/* && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl)*/)
 61         {
 62             desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate*0.125f * Mathf.Abs(desiredDistance);
 63         }
 64         // If middle mouse and left alt are selected? ORBIT
 65         else if (Input.GetMouseButton(0) /*&& Input.GetKey(KeyCode.LeftAlt)*/)
 66         {
 67             xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.01f;
 68             yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.01f;
 69             ////////OrbitAngle
 70             //Clamp the vertical axis for the orbit
 71             yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
 72             // set camera rotation 
 73             desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
 74             currentRotation = transform.rotation;
 75             
 76             rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
 77             transform.rotation = rotation;
 78            // ispingyi = true;
 79         }
 80         if (Input.GetMouseButtonUp(0))
 81         {
 82           //  ispingyi = false;
 83         }
 84         // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
 85         //else if (Input.GetMouseButton(0))
 86         //{
 87         //    //grab the rotation of the camera so we can move in a psuedo local XY space
 88         //    target.rotation = transform.rotation;
 89         //    target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
 90         //    target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
 91         //}
 92         ////////Orbit Position
 93         // affect the desired Zoom distance if we roll the scrollwheel
 94         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
 95         //clamp the zoom min/max
 96         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
 97         // For smoothing of the zoom, lerp distance
 98         currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
 99         // calculate position based on the new currentDistance 
100         
101          position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
102          transform.position = position;
103          
104  
105        
106         //if (isshang == true)
107         //{
108         //    target.Translate(transform.up * panSpeed, Space.World);
109         //}
110         //if (isxia == true)
111         //{
112         //    target.Translate(transform.up * -panSpeed, Space.World);
113         //}
114         //if (iszuo == true)
115         //{
116         //   target.transform.Translate(Vector3.right * -panSpeed);
117 
118         //}
119         //if (isyou == true)
120         //{
121         //    target.Translate(Vector3.right * panSpeed);
122         //}
123     }
124 
125     //void shang()
126     //{   
127     //    isshang = true;
128     //}
129     //void bushang()
130     //{
131     //    isshang = false;
132     //}
133     //void xia()
134     //{
135 
136     //    isxia = true;
137     //}
138     //void buxia()
139     //{
140 
141     //    isxia = false;
142     //}
143     //void zuo()
144     //{
145     //    iszuo = true;
146     //}
147     //void buzuo()
148     //{
149     //    iszuo = false;
150     //}
151     //void you()
152     //{
153     //    isyou = true;
154     //}
155     //void buyou()
156     //{
157     //    isyou = false;
158     //}
159 
160     private static float ClampAngle(float angle, float min, float max)
161     {
162         if (angle < -360)
163             angle += 360;
164         if (angle > 360)
165             angle -= 360;
166         return Mathf.Clamp(angle, min, max);
167     }
168 }

 

相关文章: