触摸屏手势控制镜头旋转与缩放
生活随笔
收集整理的這篇文章主要介紹了
触摸屏手势控制镜头旋转与缩放
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| 本帖最后由 我是一頭小毛驢 于 2016-2-26 20:45 編輯 //用于綁定參照物對象? ? ? var target : Transform;?? ? ? //縮放系數?? ? ? var distance = 10.0;?? ? ? //左右滑動移動速度?? ? ? var xSpeed = 250.0;?? ? ? var ySpeed = 120.0;?? ? ? //縮放限制系數?? ? ? var yMinLimit = -20;?? ? ? var yMaxLimit = 80;?? ? ? //攝像頭的位置?? ? ? var x = 0.0;??? ? ? ?? ? ? var y = 0.0;??? ? ? ?? ? ? //記錄上一次手機觸摸位置判斷用戶是在左放大還是縮小手勢?? ? ? private var oldPosition1 : Vector2;?? ? ? private var oldPosition2 : Vector2;?? ? ? //初始化游戲信息設置?? ? ? function Start () {?? ? ?? ???var angles = transform.eulerAngles;?? ? ?? ???x = angles.y;??? ? ? ?? ? ?? ???y = angles.x;?? ? ?? ???// Make the rigid body not change rotation?? ? ?? ???if (rigidbody)?? ? ?? ?? ?? ?rigidbody.freezeRotation = true;?? ? ? }?? ? ? function Update ()??? ? ? ?? ? ? {?? ? ?? ???//判斷觸摸數量為單點觸摸?? ? ?? ???if(Input.touchCount == 1)?? ? ?? ???{?? ? ?? ?? ?? ?//觸摸類型為移動觸摸?? ? ?? ?? ?? ?if(Input.GetTouch(0).phase==TouchPhase.Moved)?? ? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ? //根據觸摸點計算X與Y位置?? ? ?? ?? ?? ?? ? x += Input.GetAxis("Mouse X") * xSpeed * 0.02;?? ? ?? ?? ?? ?? ? y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;?? ? ?? ?? ?? ?}?? ? ?? ???}?? ? ?? ???//判斷觸摸數量為多點觸摸?? ? ?? ???if(Input.touchCount >1 )?? ? ?? ???{?? ? ?? ?? ?? ?//前兩只手指觸摸類型都為移動觸摸?? ? ?? ?? ?? ?if(Input.GetTouch(0).phase==TouchPhase.Moved??Input.GetTouch(1).phase==TouchPhase.Moved)?? ? ?? ?? ?? ?{??//<a href="http://www.taidous.com/bbs/tag-name-%3Ca%20href=" http:="" www.taidous.com="" bbs="" tag-name-unity3d.html"="" target="_blank" class="relatedlink" style="word-wrap: break-word; color: rgb(255, 102, 0) !important; text-decoration: none !important; border-bottom-style: none; margin: 0px 5px 0px 0px; padding: 0px 14px 0px 5px; vertical-align: 0px; display: inline-block; word-break: break-all; background-image: url(http://www.taidous.com/assets/img/common/linkiocn.png); background-position: 100% 45%; background-repeat: no-repeat;">Unity3d教程.html" target="_blank" class="relatedlink">Unity3d教程手冊:www.manew.com ? ?? ?? ?? ?? ?? ???//計算出當前兩點觸摸點的位置?? ? ?? ?? ?? ?? ?? ???var tempPosition1 = Input.GetTouch(0).position;?? ? ?? ?? ?? ?? ?? ???var tempPosition2 = Input.GetTouch(1).position;?? ? ?? ?? ?? ?? ?? ???//函數返回真為放大,返回假為縮小?? ? ?? ?? ?? ?? ?? ???if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2))?? ? ?? ?? ?? ?? ?? ???{?? ? ?? ?? ?? ?? ?? ?? ?? ?//放大系數超過3以后不允許繼續放大?? ? ?? ?? ?? ?? ?? ?? ?? ?//這里的數據是根據我項目中的模型而調節的,大家可以自己任意修改?? ? ?? ?? ?? ?? ?? ?? ?? ?if(distance > 3)?? ? ?? ?? ?? ?? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ?? ?? ?? ?? ? distance -= 0.5;??? ? ? ?? ? ?? ?? ?? ?? ?? ?? ?? ?}??? ? ? ?? ? ?? ?? ?? ?? ?? ???}else{?? ? ?? ?? ?? ?? ?? ?? ?? ?//縮小洗漱返回18.5后不允許繼續縮小?? ? ?? ?? ?? ?? ?? ?? ?? ?//這里的數據是根據我項目中的模型而調節的,大家可以自己任意修改?? ? ?? ?? ?? ?? ?? ?? ?? ?if(distance < 18.5)?? ? ?? ?? ?? ?? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ?? ?? ?? ?? ? distance += 0.5;?? ? ?? ?? ?? ?? ?? ?? ?? ?}??? ? ? ?? ? ?? ?? ?? ?? ?? ???}??? ? ? ?? ? ?? ?? ?? ?? ? //備份上一次觸摸點的位置,用于對比??? ? ? ?? ? ?? ?? ?? ?? ? oldPosition1=tempPosition1;??? ? ? ?? ? ?? ?? ?? ?? ? oldPosition2=tempPosition2;??? ? ? ?? ? ?? ?? ?? ?}??? ? ? ?? ? ?? ???}?? ? ? }?? ? ? //函數返回真為放大,返回假為縮小?? ? ? function isEnlarge(oP1 : Vector2,oP2 : Vector2,nP1 : Vector2,nP2 : Vector2) : boolean??? ? ? ?? ? ? {?? ? ?? ???//函數傳入上一次觸摸兩點的位置與本次觸摸兩點的位置計算出用戶的手勢?? ? ?? ???var leng1 =Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));? ? ?? ???var leng2 =Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));?? ? ?? ???if(leng1<leng2)?? ? ?? ???{?? ? ?? ?? ?? ? //放大手勢?? ? ?? ?? ?? ? return true;?? ? ?? ???}else{?? ? ?? ?? ?? ?//縮小手勢?? ? ?? ?? ?? ?return false;?? ? ?? ???}?? ? ? }?? ? ? //Update方法一旦調用結束以后進入這里算出重置攝像機的位置?? ? ? function LateUpdate () {?? ? ?? ???//target為我們綁定的箱子變量,縮放旋轉的參照物?? ? ?? ???if (target) {? ?? ?? ? ? ?? ?? ?? ?//重置攝像機的位置?? ? ?? ?? ?? ?y = ClampAngle(y, yMinLimit, yMaxLimit);?? ? ?? ?? ?? ?var rotation = Quaternion.Euler(y, x, 0);?? ? ?? ?? ?? ?var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;?? ? ?? ?? ?? ?transform.rotation = rotation;?? ? ?? ?? ?? ?transform.position = position;?? ? ?? ???}?? ? ? }?? ? ? static function ClampAngle (angle : float, min : float, max : float) {?? ? ?? ???if (angle < -360)?? ? ?? ?? ?? ?angle += 360;?? ? ?? ???if (angle > 360)??? ? ? ?? ? ?? ?? ?? ?angle -= 360;??? ? ? ?? ? ?? ???return Mathf.Clamp (angle, min, max);?? ? ? }?? C#版本并加上y限制。 ? ? using UnityEngine;?? ? ? using System.Collections;?? ? ? public class Move : MonoBehaviour {?? ? ? public GameObject target;?? ? ? public float distance = 10.0f;?? ? ? public float xSpeed = 250.0f;?? ? ? public float ySpeed = 120.0f;?? ? ? Vector3 tmp;?? ? ? public float yMinLimit = -20;?? ? ? public float yMaxLimit = 80;?? ? ? public float x = 0.0f;?? ? ? public float y = 0.0f;?? ? ? private Vector2 oldPosition1;?? ? ? private Vector2 oldPosition2;?? ? ? void Start (){?? ? ?? ???Vector2 angles= transform.eulerAngles;?? ? ?? ???x = angles.y;?? ? ?? ???y = angles.x;?? ? ?? ???// Make the rigid body not change rotation?? ? ?? ???if (rigidbody)?? ? ?? ?? ?? ?rigidbody.freezeRotation = true;?? ? ? }?? ? ? void Update (){?? ? ?? ???if(Input.touchCount == 1)?? ? ?? ???{?? ? ?? ?? ?? ?if(Input.GetTouch(0).phase==TouchPhase.Moved)?? ? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ? x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;?? ? ?? ?? ?? ?? ? y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;?? ? ?? ?? ?? ?? ? y=ClampAngle(y,yMinLimit,yMaxLimit);?? ? ?? ?? ?? ?}?? ? ?? ???}?? ? ?? ???if(Input.touchCount >1 )?? ? ?? ???{?? ? ?? ?? ?? ?if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved)?? ? ?? ?? ?? ?{? ? ?? ?? ?? ?? ?? ???Vector3 tempPosition1= Input.GetTouch(0).position;?? ? ?? ?? ?? ?? ?? ???Vector3 tempPosition2= Input.GetTouch(1).position;?? ? ?? ?? ?? ?? ?? ???if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2))?? ? ?? ?? ?? ?? ?? ???{?? ? ?? ?? ?? ?? ?? ?? ?? ?if(distance > 3)?? ? ?? ?? ?? ?? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ?? ?? ?? ?? ? distance -= 0.5f;? ??? ? ?? ?? ?? ?? ?? ?? ?? ?} ? ?? ?? ?? ?? ?? ???}else{?? ? ?? ?? ?? ?? ?? ?? ?? ?if(distance < 18.5f)?? ? ?? ?? ?? ?? ?? ?? ?? ?{?? ? ?? ?? ?? ?? ?? ?? ?? ?? ? distance += 0.5f;?? ? ?? ?? ?? ?? ?? ?? ?? ?}?? ? ?? ?? ?? ?? ?? ???}?? ? ?? ?? ?? ?? ? oldPosition1=tempPosition1;?? ? ?? ?? ?? ?? ? oldPosition2=tempPosition2;?? ? ?? ?? ?? ?}?? ? ?? ???}?? ? ? }?? ? ?? ?? ?bool isEnlarge ( Vector2 oP1 ,??Vector2 oP2 ,??Vector2 nP1 ,??Vector2 nP2??)?? ? ?? ???{?? ? ?? ?? ?float leng1=Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));?? ? ?? ?? ?float leng2=Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));?? ? ?? ???if(leng1<leng2)?? ? ?? ???{?? ? ?? ?? ?? ? return true;? ? ? ?? ???}else{?? ? ?? ?? ?? ?return false;? ? ? ?? ???}?? ? ? } ? ?? ???public void LateUpdate (){?? ? ?? ???if (target) {? ?? ?? ? ? ?? ?? ?? ?ClampAngle(y, yMinLimit, yMaxLimit);?? ? ?? ?? ?? ?Quaternion rotation= Quaternion.Euler(y, x, 0);?? ? ?? ?? ?? ?tmp.Set(0.0f, 0.0f, (-1)*distance);? ? ? ?? ?? ?? ?Vector3 position= rotation * tmp + target.transform.position;? ? ?? ?? ?? ?transform.rotation = rotation;?? ? ?? ?? ?? ?transform.position = position;?? ? ?? ???}?? ? ? }?? ? ?? ???static float ClampAngle ( float angle ,? ?float min ,? ?float max??){?? ? ?? ???if (angle < -360)?? ? ?? ?? ?? ?angle += 360;?? ? ?? ???if (angle > 360)?? ? ?? ?? ?? ?angle -= 360;?? ? ?? ???return Mathf.Clamp (angle, min, max);?? ? ?? ???}?? ? ? }?? |
總結
以上是生活随笔為你收集整理的触摸屏手势控制镜头旋转与缩放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CATV频率表
- 下一篇: 【Proteus仿真】【51单片机】水箱