Unity3d--控制摄像机的视野范围
Posted 昱歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Unity3d--控制摄像机的视野范围的主要内容,如果未能解决你的问题,请参考以下文章
unity3d摄像机角度怎么调整啊?u3d如何调整摄像机的位置?求解