运行时可手动旋转摄像机脚本

首先把摄像机设置成摄像机盒子,如图

Obj XuNiTi  是3Dmax导出的fbx添加虚拟体如果有虚拟体,相机可以跟随虚拟体运行,FollowState 状态为1

Angles 是相机的旋转角度  可代码设置

Angles Speed 相机旋转速度

TargetHeight 目标高度

DistanceSpeed 缩放时的速度

Distance 相机初始位置  0.1是最小距离  30是最大距离

FollowState  0 定点的不能移动旋转  1 跟随虚拟体  2 自由视角

脚本如下 挂在CameraCube上即可

  1 using UnityEngine;
  2 using Unity;
  3 
  4 /// <summary>
  5 /// 摄像机跟随 虚拟体
  6 /// </summary>
  7 public class CameraFollow : MonoBehaviour
  8 {
  9     public static CameraFollow instrance;
 10     public GameObject objXuNiTi;
 11     //摄像机旋转角度
 12     public Vector3 angles = Vector3.zero;
 13     public Vector3 anglesSpeed = new Vector3(10, 10, 5);
 14     public Vector2 anglesDu = new Vector2(-80, 80);
 15     //摄像机距离
 16     public float targetHeight = 1, distanceSpeed = 5;
 17     public Vector3 distance = new Vector3(0, 10, 15);
 18 
 19     private float moveSpeed;
 20     private Transform objCamera;
 21     private Transform objPivot;
 22     private float dist;
 23     private bool isCorrected;
 24     //--------------------------------
 25     /// <summary>
 26     /// 摄像模式:是否0:1定点/2漫游
 27     /// </summary>
 28     public int FollowState = 0;
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36     void Awake()
 37     {
 38         instrance = this;
 39         angles = transform.eulerAngles;
 40         objCamera = gameObject.GetComponentInChildren<Camera>().transform;
 41         objPivot = objCamera.parent;
 42     }
 43     void LateUpdate()
 44     {
 45         if (FollowState == 1)
 46         {
 47             this.transform.position = this.objXuNiTi.transform.position;
 48         }
 49         if (UICamera.hoveredObject != null && UICamera.hoveredObject.name != "UI Root" && UICamera.hoveredObject.name != "CursorBox") return;
 50         //定点模式
 51         if (FollowState == 1)
 52         {
 53             this.SetCameraRote();//设置摄像机旋转角度
 54             this.SetCameraDistance();//设置摄像机与物体距离
 55         }
 56         else if (FollowState == 2)
 57         {
 58             this.SetCameraRote();//设置摄像机旋转角度
 59             this.CameCubeMove();//摄像机盒子自由移动
 60             this.SetCameraDistance();//设置摄像机与物体距离
 61         }
 62     }
 63 
 64 
 65 
 66 
 67 
 68 
 69     //---------------------------------------------------------------------------------------------------------------
 70     /// <summary>
 71     /// 定点模式下,调整摄像机距离
 72     /// </summary>
 73     public void SetCameraDistance(float _dis)
 74     {
 75         if (this.FollowState == 1 && _dis > 0)
 76         {
 77             this.distance.y = _dis;
 78         }
 79     }
 80     /// <summary>
 81     /// 定点模式下,调整摄像机角度
 82     /// </summary>
 83     public void SetCameraAngle(Vector3 _vet)
 84     {
 85         if (this.FollowState != 2 && _vet != Vector3.zero)
 86         {
 87             this.angles = _vet;
 88         }
 89     }
 90     /// <summary>
 91     /// 设置物体移动到虚拟体
 92     /// </summary>
 93     /// <param name="_isMove">是否有移动动画</param>
 94     public void SetCameraXuNiTi(GameObject _xuNiTi, bool _isMove)
 95     {
 96         if (_xuNiTi == null)
 97         {
 98             Debug.Log("【确实关联】没有给摄像机指定虚拟体对象!");
 99             return;
100         }
101 
102         this.objXuNiTi = _xuNiTi;
103         this.transform.position = this.objXuNiTi.transform.position;
104         if (_isMove == false)
105             this.CameraMoveEnd();
106         else
107         {
108             this.transform.Translate(Vector3.forward * -30);
109             this.transform.Translate(Vector3.up * 15);
110             iTween.MoveTo(this.gameObject, iTween.Hash("position", objXuNiTi.transform.position, "time", 2, "easetype", "linear", "oncomplete", "CameraMoveEnd"));
111         }
112     }
113     private void CameraMoveEnd()
114     {
115         this.FollowState = 1;//设置定点模式
116         AnimatorMediator.Instance.AnimaBegin();
117     }
118 
119 
120 
121 
122 
123     //---------------------------------------------------------------------------------------------------------------
124     /// <summary>
125     /// 摄像机盒子自由移动
126     /// </summary>
127     private void CameCubeMove()
128     {
129         if (Input.GetMouseButton(2))
130         {
131             moveSpeed = Vector3.Distance(transform.position, objCamera.transform.position) / 40;
132             transform.Translate(objCamera.up * (-Input.GetAxis("Mouse Y")) * moveSpeed, Space.World);
133             transform.Translate(Vector3.right * (-Input.GetAxis("Mouse X")) * moveSpeed, Space.Self);
134         }
135     }
136     /// <summary>
137     /// 设置摄像机旋转角度
138     /// </summary>
139     private void SetCameraRote()
140     {
141         if (Input.GetMouseButton(1))
142         {
143             angles.x -= anglesSpeed.x * Input.GetAxis("Mouse Y");
144             angles.y += anglesSpeed.y * Input.GetAxis("Mouse X");
145             angles.x = Mathf.Clamp(angles.x, anglesDu.x, anglesDu.y);
146         }
147         transform.rotation = Quaternion.Euler(0f, angles.y, 0f);
148         this.objPivot.localRotation = Quaternion.Euler(angles.x, 0f, 0f);
149     }
150     /// <summary>
151     /// 设置摄像机与物体距离
152     /// </summary>
153     private void SetCameraDistance()
154     {
155         dist = Vector3.Distance(objCamera.position, objPivot.position);
156         if (Input.GetAxis("Mouse ScrollWheel") > 0)
157             distance.y -= Time.deltaTime * dist * distanceSpeed;
158         else if (Input.GetAxis("Mouse ScrollWheel") < 0)
159             distance.y += Time.deltaTime * dist * distanceSpeed;
160         distance.y = Mathf.Clamp(distance.y, distance.x, distance.z);
161         Vector3 position = objPivot.position - (objPivot.rotation * Vector3.forward * distance.y + new Vector3(0, -targetHeight, 0));
162 
163         isCorrected = false;
164         RaycastHit collisionHit;
165         if (Physics.Linecast(objPivot.position, position, out collisionHit, 1 << LayerMask.NameToLayer("Default")))
166         {
167             position = collisionHit.point;
168             isCorrected = true;
169         }
170         if (isCorrected)
171         {
172             Debug.DrawRay(objPivot.position, -transform.forward * Camera.main.transform.localPosition.magnitude, Color.red);
173         }
174         objCamera.position = position + new Vector3(0, 0.1f, 0);
175     }
176 }
View Code

 

第二个版本 

只需要MainCamera 脚本挂上即可

脚本如下

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class RotateCamera : MonoBehaviour
 5 {
 6     public Transform target;
 7 
 8     //距离
 9     public float distanceX = 0;
10     public float distanceY = 0;
11     public float distanceZ=-5;
12     public float minDistance = -2;
13     public float maxDistance =-50;
14 
15     //角度
16     public float angleX=30f;
17     public float angleY=0;
18     public float angleZ=0;
19 
20     public float angleSpeed = 5f;
21     public float scrollSpeed = 5f;
22     public float moveSpeed = 5f;
23 
24     // Use this for initialization
25     void Start ()
26     {
27         Debug.Log(Quaternion.Euler(0, 90, 0));
28        Debug.Log(Quaternion.Euler(0, 90, 0)*new Vector3(0, 0, -10));
29     }
30     
31     // Update is called once per frame
32     void Update ()
33     {
34         Mouse();
35         UpdateCamera();
36     }
37 
38     private void Mouse()
39     {
40         if (Input.GetMouseButton(1))
41         {
42             angleY += Input.GetAxis("Mouse X")*angleSpeed;
43             angleX -= Input.GetAxis("Mouse Y")*angleSpeed;
44         }
45         if (Input.GetAxis("Mouse ScrollWheel") != 0)
46         {
47             //根据鼠标滚轮的值计算出这一帧应该移动的距离。
48             distanceZ += Mathf.Lerp(0, Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, Time.deltaTime);
49             distanceZ = Mathf.Clamp(distanceZ,maxDistance, minDistance);
50         }
51         if (Input.GetMouseButton(2))
52         {
53             //根据鼠标滑动的值计算出这一帧一个移动的距离
54             distanceX -= Mathf.Lerp(0, Input.GetAxis("Mouse X") * moveSpeed, Time.deltaTime);
55             distanceY -= Mathf.Lerp(0, Input.GetAxis("Mouse Y") * moveSpeed, Time.deltaTime);
56         }
57     }
58 
59     private void UpdateCamera()
60     {
61         Quaternion rotation=Quaternion.Euler(angleX,angleY,angleZ);
62 
63         //创建一个位移变量
64         Vector3 pos=new Vector3(distanceX,distanceY,distanceZ);
65 
66         Vector3 postion = rotation * pos;
67         if (target != null)
68             postion += target.position;
69 
70         transform.position = postion;
71         transform.rotation = rotation;
72     }
73 }
View Code

 

 

树欲静而风不止,子欲养而亲不待。

2016年12月8日09:32:34

 

欢迎广大unity爱好者进群学习  165628892

posted @ 2016-12-08 09:33  叫我张先生  阅读(598)  评论(0编辑  收藏  举报