日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity通过鼠标或者手势实现拉进拉远,旋转等操作的常用方法

發(fā)布時(shí)間:2024/3/13 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity通过鼠标或者手势实现拉进拉远,旋转等操作的常用方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在項(xiàng)目制作過程中,我們可能會用到鏡頭的拉伸與旋轉(zhuǎn)操作,不管在pc端還是移動端,拉伸效果要么就是放大target,要么就是移動camera,旋轉(zhuǎn)也是一樣,要么旋轉(zhuǎn)攝像機(jī),要么是旋轉(zhuǎn)物體本身,具體情況具體對待。下面就兩個(gè)方法的具體實(shí)現(xiàn)方法:


下面是通過控制camera與target的距離,實(shí)時(shí)更新camera的位置來實(shí)現(xiàn)物體的方法縮小,也就是拉伸效果。同樣旋轉(zhuǎn)物體也是通過setpos方法,實(shí)時(shí)更新位置實(shí)現(xiàn)攝像機(jī)的旋轉(zhuǎn)。這種拉伸效果適合用在target是三維模型時(shí)。

using UnityEngine; using System.Collections;public class RotateAndPinch_2 : MonoBehaviour {public Transform target;private float minVertical = 0f;private float maxVertical = 85f;private float x = 0.0f;private float y = 0.0f;private float distance = 0.0f;private float newdis = 0;private float olddis = 0;// Use this for initializationvoid Start(){distance = (transform.position - target.position).magnitude;}// Update is called once per framevoid Update(){transform.LookAt(target);float dt = Time.deltaTime;x = transform.eulerAngles.y;y = transform.eulerAngles.x;if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer){if (Input.touchCount == 1){if (Input.GetTouch(0).phase == TouchPhase.Moved){float x1 = Input.GetAxis("Mouse X");float y1 = Input.GetAxis("Mouse Y");x += x1 * dt * 150;y += -y1 * dt * 150;SetPos(x, y);}}if (Input.touchCount == 2){if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved){Vector3 s1 = Input.GetTouch(0).position;Vector3 s2 = Input.GetTouch(1).position;newdis=Vector2.Distance(s1,s2);if(newdis>olddis){distance-=Time.deltaTime*50f;}if(newdis<olddis){distance+=Time.deltaTime*50f;}print("distance = "+distance);SetPos(x,y);olddis=newdis;}}}else{if (Input.GetMouseButton(0)){float x1 = Input.GetAxis("Mouse X");float y1 = Input.GetAxis("Mouse Y");x += x1 * dt * 150f;y += -y1 * dt * 150f;SetPos(x, y);}if (Input.GetAxis("Mouse ScrollWheel") != 0){distance -= Input.GetAxis("Mouse ScrollWheel");SetPos(x, y);}}}void SetPos(float x, float y){y = ClampAngle(y, minVertical, maxVertical);var rotation = Quaternion.Euler(y, x, 0.0f);var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.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);}}

下面這個(gè)方法,實(shí)現(xiàn)上則比上面的簡單一點(diǎn),放大縮小是通過改變scale來實(shí)現(xiàn),這種方法使用在二維平面中。

using UnityEngine;/// <summary> /// 旋轉(zhuǎn)和縮放模型 /// </summary> public class ScaleOrRotateModels : MonoBehaviour {//最大和最小縮放比例值private float maxScale = 100;private float minScale = 20;private float newdis = 0;private float olddis = 0;private float horizontalSpeed = 5;private float verticalSpeed = 5;private Vector3 rotatepos;private Vector3 defaultScale;void Start(){rotatepos = transform.position;defaultScale = transform.localScale;}void LateUpdate(){if (Application.platform == RuntimePlatform.Android){//單指觸屏滑動,物體的旋轉(zhuǎn)if (Input.touchCount == 1){if (Input.GetTouch(0).phase == TouchPhase.Moved){float h = Input.GetAxis("Mouse X");//右正左負(fù) float v = Input.GetAxis("Mouse Y");//上正下負(fù) if (Mathf.Abs(h) >= Mathf.Abs(v)){if (h < 0){transform.RotateAround(rotatepos, Vector3.up, horizontalSpeed);}if (h > 0){transform.RotateAround(rotatepos, -Vector3.up, horizontalSpeed);}}else{if (v < 0){transform.RotateAround(rotatepos, -Vector3.right, verticalSpeed);}if (v > 0){transform.RotateAround(rotatepos, Vector3.right, verticalSpeed);}}}}//兩指觸屏滑動,物體的縮放 if (Input.touchCount > 1){if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved){var pos1 = Input.GetTouch(0).position;var pos2 = Input.GetTouch(1).position;newdis = Vector2.Distance(pos1, pos2);if (newdis < olddis && transform.localScale.x > minScale){transform.localScale -= defaultScale * Time.deltaTime * 2;}if (newdis > olddis && transform.localScale.x < maxScale){transform.localScale += defaultScale * Time.deltaTime * 2;}olddis = newdis;}}}else{//鼠標(biāo)左鍵,物體旋轉(zhuǎn)if (Input.GetMouseButton(0)) {float h = Input.GetAxis("Mouse X");//右正左負(fù) float v = Input.GetAxis("Mouse Y");//上正下負(fù) if (Mathf.Abs(h) >= Mathf.Abs(v)){if (h < 0){transform.RotateAround(rotatepos, Vector3.up, horizontalSpeed);}if (h > 0){transform.RotateAround(rotatepos, -Vector3.up, horizontalSpeed);}}else{if (v < 0){transform.RotateAround(rotatepos, -Vector3.right, verticalSpeed);}if (v > 0){transform.RotateAround(rotatepos, Vector3.right, verticalSpeed);}}}if (Input.GetAxis("Mouse ScrollWheel") < 0 && transform.localScale.x < maxScale){transform.localScale += defaultScale * Time.deltaTime * 2;}else if (Input.GetAxis("Mouse ScrollWheel") > 0 && transform.localScale.x > minScale){transform.localScale -= defaultScale * Time.deltaTime * 2;}}} }



這是我常用的2種方法,有什么錯(cuò)誤的地方 歡迎大家指正!!!


總結(jié)

以上是生活随笔為你收集整理的Unity通过鼠标或者手势实现拉进拉远,旋转等操作的常用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。