Unity3d--控制攝像機的視野範圍

  1 using UnityEngine;
  2 using System.Collections;
  3 /*
  4  * 控制攝像機的視野範圍
  5 */
  6 public class CameFieldCS : MonoBehaviour {
  7 
  8     private Transform player;
  9     private Vector3 offsetPosition;
 10     private float distance;
 11     private float scrollSpeed = 10; //鼠標滾輪速度
 12     private bool isRotating; //開啓攝像機旋轉
 13     private float rotateSpeed = 2; //攝像機旋轉速度
 14 
 15     private float speed = 10;
 16     private float endZ = -8;
 17 
 18     private Camera came;
 19     // Use this for initialization
 20 
 21     void Awake(){
 22         player = GameObject.FindGameObjectWithTag ("Player").transform;
 23         came = this.GetComponent<Camera> ();
 24     }
 25 
 26     void Start () {
 27         //攝像機朝向player
 28         transform.LookAt (player.position);
 29         //獲取攝像機與player的位置偏移
 30         offsetPosition = transform.position - player.position;
 31     }
 32 
 33     // Update is called once per frame
 34     void Update () {
 35         //攝像機跟隨player與player保持相對位置偏移 
 36         transform.position = offsetPosition + player.position;
 37 
 38         //攝像機視野範圍控制
 39  ScrollView ();  40 
 41         //攝像機的旋轉
 42         RotateView ();
 43     }
 44 
 45     void ScrollView(){  46         //放大視野
 47         if (Input.GetAxis("Mouse ScrollWheel") < 0) {  48             if(came.fieldOfView <= 70) {  49                 came.fieldOfView += 5;  50  }  51  }  52 
 53         //縮小視野
 54         if (Input.GetAxis("Mouse ScrollWheel") > 0) {  55             if(came.fieldOfView >= 30) {  56                 came.fieldOfView -= 5;  57  }  58  }  59  }  60 
 61     void RotateView(){
 62         //獲取鼠標在水平方向的滑動
 63         Debug.Log(Input.GetAxis ("Mouse X"));
 64         //獲取鼠標在垂直方向的滑動
 65         Debug.Log(Input.GetAxis("Mouse Y"));
 66 
 67         //按下鼠標右鍵開啓旋轉攝像機
 68         if (Input.GetMouseButtonDown(1)) {
 69             isRotating = true;
 70         }
 71 
 72         //擡起鼠標右鍵關閉旋轉攝像機
 73         if (Input.GetMouseButtonUp(1)) {
 74             isRotating = false;
 75         }
 76 
 77         if (isRotating) {
 78 
 79             //獲取攝像機初始位置
 80             Vector3 pos = transform.position;
 81             //獲取攝像機初始角度
 82             Quaternion rot = transform.rotation;
 83 
 84             //攝像機圍繞player的位置延player的Y軸旋轉,旋轉的速度爲鼠標水平滑動的速度
 85             transform.RotateAround(player.position,player.up,Input.GetAxis("Mouse X") * rotateSpeed);
 86 
 87             //攝像機圍繞player的位置延自身的X軸旋轉,旋轉的速度爲鼠標垂直滑動的速度
 88             transform.RotateAround (player.position, transform.right, Input.GetAxis ("Mouse Y") * rotateSpeed);
 89 
 90             //獲取攝像機x軸向的歐拉角
 91             float x = transform.eulerAngles.x;
 92 
 93             //若是攝像機的x軸旋轉角度超出範圍,恢復初始位置和角度
 94             if (x<10 || x>80) {
 95                 transform.position = pos;
 96                 transform.rotation = rot;
 97             }
 98         }
 99 
100         //更新攝像機與player的位置偏移
101         offsetPosition = transform.position - player.position;
102     }
103 }
相關文章
相關標籤/搜索