好了,接下來我們來創建一個名爲FreeView的腳本,該腳本依附在主相機上,下面我們來看它的實現過程:
- using UnityEngine;
- using System.Collections;
- public class FreeView : MonoBehaviour {
- //觀察目標
- public Transform Target;
- //觀察距離
- public float Distance = 5F;
- //旋轉速度
- private float SpeedX=240;
- private float SpeedY=120;
- //角度限制
- private float MinLimitY = -180;
- private float MaxLimitY = 180;
- //旋轉角度
- private float mX = 0.0F;
- private float mY = 0.0F;
- //鼠標縮放距離最值
- private float MaxDistance=10;
- private float MinDistance=1.5F;
- //鼠標縮放速率
- private float ZoomSpeed=2F;
- //是否啓用差值
- public bool isNeedDamping=true;
- //速度
- public float Damping=10F;
- //存儲角度的四元數
- private Quaternion mRotation;
- //定義鼠標按鍵枚舉
- private enum MouseButton
- {
- //鼠標左鍵
- MouseButton_Left=0,
- //鼠標右鍵
- MouseButton_Right=1,
- //鼠標中鍵
- MouseButton_Midle=2
- }
- //相機移動速度
- private float MoveSpeed=5.0F;
- //屏幕座標
- private Vector3 mScreenPoint;
- //座標偏移
- private Vector3 mOffset;
- void Start ()
- {
- //初始化旋轉角度
- mX=transform.eulerAngles.x;
- mY=transform.eulerAngles.y;
- }
- void LateUpdate ()
- {
- //鼠標右鍵旋轉
- if(Target!=null && Input.GetMouseButton((int)MouseButton.MouseButton_Right))
- {
- //獲取鼠標輸入
- mX += Input.GetAxis("Mouse X") * SpeedX * 0.02F;
- mY -= Input.GetAxis("Mouse Y") * SpeedY * 0.02F;
- //範圍限制
- mY = ClampAngle(mY,MinLimitY,MaxLimitY);
- //計算旋轉
- mRotation = Quaternion.Euler(mY, mX, 0);
- //根據是否插值採取不同的角度計算方式
- if(isNeedDamping){
- transform.rotation = Quaternion.Lerp(transform.rotation,mRotation, Time.deltaTime*Damping);
- }else{
- transform.rotation = mRotation;
- }
- }
- //鼠標中鍵平移
- //鼠標滾輪縮放
- Distance-=Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
- Distance=Mathf.Clamp(Distance,MinDistance,MaxDistance);
- //重新計算位置
- Vector3 mPosition = mRotation * new Vector3(0.0F, 0.0F, -Distance) + Target.position;
- //設置相機的位置
- if(isNeedDamping){
- transform.position = Vector3.Lerp(transform.position,mPosition, Time.deltaTime*Damping);
- }else{
- transform.position = mPosition;
- }
- }
- //角度限制
- private float ClampAngle (float angle,float min,float max)
- {
- if (angle < -360) angle += 360;
- if (angle > 360) angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
- }
大家應該注意到博主沒有實現按下鼠標中鍵平移的相關方法,因爲這部分博主目前還沒有徹底正確地寫出來,如果大家知道的話,可以給博主留言,或者等博主什麼時候想清楚了再來更新這篇博客吧。博主的博客會經常更新的,希望大家繼續關注我的博客吧。好了,最後我們再來做一個遷移,我們將把這個功能移植到Android平臺上,本來想把這部分作爲Android手勢控制單獨寫一篇文章的,可是感覺這裏的代碼都是相似的,再寫一篇實在沒有必要,所以,就直接給出代碼吧!
- using UnityEngine;
- using System.Collections;
- public class MobileInput : MonoBehaviour {
- //觀察目標
- public Transform Target;
- //觀察距離
- public float Distance = 2F;
- //距離最值
- public float MaxDistance=5F;
- public float MinDistance=0.5F;
- //縮放速率
- private float ZoomSpeed=0.15F;
- //旋轉速度
- private float SpeedX=240;
- private float SpeedY=120;
- //角度限制
- private float MinLimitY = 5;
- private float MaxLimitY = 180;
- //旋轉角度
- private float mX=0;
- private float mY=0;
- //當前手勢
- private Vector2 mPos;
- void Start ()
- {
- //允許多點觸控
- Input.multiTouchEnabled=true;
- //初始化旋轉
- mX=Target.eulerAngles.x;
- mY=Target.eulerAngles.y;
- }
- void Update ()
- {
- if(!Target) return;
- //單點觸控
- if(Input.touchCount==1)
- {
- //手指處於移動狀態
- if(Input.touches[0].phase==TouchPhase.Moved)
- {
- mX+=Input.GetAxis("Mouse X") * SpeedX * 0.02F;
- mY-=Input.GetAxis("Mouse X") * SpeedY * 0.02F;
- mY=ClampAngle(mY,MinLimitY,MaxLimitY);
- }
- }
- //多點觸控
- if(Input.touchCount>1)
- {
- //兩隻手指都處於移動狀態
- if(Input.touches[0].phase==TouchPhase.Moved || Input.touches[1].phase==TouchPhase.Moved)
- {
- //計算移動方向
- Vector2 mDir=Input.touches[1].position-Input.touches[0].position;
- //根據向量的大小判斷當前手勢是放大還是縮小
- if(mDir.sqrMagnitude>mPos.sqrMagnitude){
- Distance-=ZoomSpeed;
- }else{
- Distance+=ZoomSpeed;
- }
- //限制距離
- Distance=Mathf.Clamp(Distance,MinDistance,MaxDistance);
- //更新當前手勢
- mPos=mDir;
- }
- }
- //計算相機的角度和位置
- transform.rotation=Quaternion.Euler(new Vector3(mY,mX,0));
- transform.position=transform.rotation * new Vector3(0,0,-Distance)+Target.position;
- }
- //角度限制
- private float ClampAngle (float angle,float min,float max)
- {
- if (angle < -360) angle += 360;
- if (angle > 360) angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
- }
今天的內容就是這樣子啦,博主最近在搗鼓Unity與Android對接的一些東西,等博主研究好了就分享給大家,希望大家喜歡。最後送上:
每日箴言:生活就是種律動,須有光有影,有左有右。人生就是一場無止境的漂泊。面對生活中那些觸不及防的意外、突如其來的傷痛、從天而降的災難,相信,只要有愛,就有足夠的勇氣去承受人生中所有的風雨,一切會變得輕鬆而釋然。