代码如下:
private Transform player;
public float scrollSpeed = 6.0f;
public float roundSpeed = 4.2f;
public float _vMinLimit = 15f; //y min limit
public float _vMaxLimit = 55f; //y max limit
public float zoomMin = 3f; //zoom min
public float zoomMax = 8f; //zoom max
float hh;
float vv;
float distance;
// Update is called once per frame
void Update () {
if (!player) {
getPlayer();
}
calPos (aroundView (), scrollView ());
}
float scrollView(){
float wheel = Input.GetAxis ("Mouse ScrollWheel");
float dis = Vector3.Distance (transform.position, player.position);
dis -= wheel * scrollSpeed;
distance = Mathf.Clamp (distance,zoomMin,zoomMax);
return distance;
}
Quaternion aroundView(){
float h = Input.GetAxis ("Mouse X");
float v = Input.GetAxis ("Mouse Y");
Quaternion rt = Quaternion.Euler(transform.eulerAngles);
bool rightMouseDown = Input.GetMouseButton (1);
if (rightMouseDown) {
hh += h * roundSpeed; // horizontal
vv -= v;//vertical
vv = Mathf.Clamp(vv,_vMinLimit,_vMaxLimit);
rt = Quaternion.Euler(vv,hh,0f);
}
return rt;
}
//core code
void calPos(Quaternion angle,float distance){
Vector3 pos = angle*new Vector3 (0f,0f,-distance) + player.position;//core code
transform.position = pos;
transform.rotation = angle;//or use the next method
// transform.LookAt (player.position);
}
void getPlayer(){
player = GameObject.FindGameObjectWithTag (TagsEnum.Player.ToString ()).transform;
hh = transform.eulerAngles.y;
vv = transform.eulerAngles.x;
distance = zoomMax;
}