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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

实现Unity编辑器模式下的旋转

發布時間:2025/6/17 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现Unity编辑器模式下的旋转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  最近在做一個模型展示的項目,我的想法是根據滑動屏幕的x方向差值和Y方向的差值,來根據世界坐標下的X軸和Y軸進行旋轉,但是實習時候總是有一些卡頓。在觀察unity編輯器下的旋轉之后,發現編輯器下的旋轉非常流暢。仔細觀察之后發現unity編輯器下的旋轉運算模式如下圖所示,紅色箭頭方向為觸控滑動方向,黑色箭頭為模型旋轉的軸。?

  了解原理之后就是實現相關功能,具體實現還是粘代碼吧。代碼如下

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 using UnityEngine.EventSystems; 6 7 //腳本掛在Modelshow GameObject下 8 public class ModelViewControl : MonoBehaviour { 9 private bool isClick = false; 10 private Vector3 startPos; //點下開始位置 11 private Vector3 endPos; //點下終點位置 12 private float Move_X; //x方向上的移動距離 13 private float Move_Y; //Y方向上的移動距離 14 15 //回調間距 16 float interval = 0.01f; 17 float clickBeginTime = 0.0f; 18 //模型引用 19 private Transform model; //模型根節點 20 void Start () { 21 model = transform; 22 23 } 24 // Update is called once per frame 25 void Update () { 26 #if UNITY_STANDALONE_WIN 27 if (Input.GetMouseButtonDown(0)) 28 #elif UNITY_ANDROID 29 if(Input.touchCount > 0 && !isClick) 30 #endif 31 { 32 isClick = true; 33 #if UNITY_STANDALONE_WIN 34 startPos = Input.mousePosition; 35 #elif UNITY_ANDROID 36 startPos = Input.touches[0].position; 37 #endif 38 clickBeginTime = Time.time; 39 } 40 #if UNITY_STANDALONE_WIN 41 if (Input.GetMouseButtonUp(0)) 42 #elif UNITY_ANDROID 43 if (Input.touchCount == 0 && isClick) 44 #endif 45 { 46 isClick = false; 47 } 48 if (isClick && (Time.time - clickBeginTime) > interval) 49 { 50 #if UNITY_STANDALONE_WIN 51 endPos = Input.mousePosition; 52 #elif UNITY_ANDROID 53 endPos = Input.touches[0].position; 54 #endif 55 if ((endPos - startPos).magnitude < 5) 56 { 57 return; 58 } 59 if(Mathf.Abs(endPos.x - startPos.x) < 5) 60 { 61 endPos.x = startPos.x; 62 } 63 if (Mathf.Abs(endPos.y - startPos.y) < 5) 64 { 65 endPos.y = startPos.y; 66 } 67 RotateModel(startPos,endPos); 68 startPos = endPos; 69 } 70 } 71 void RotateModel(Vector3 startPos , Vector3 endPos) 72 { 73 Vector3 direction = endPos - startPos; 74 Vector3 world_axis = Vector3.Cross(direction, Vector3.forward); 75 model.Rotate(world_axis.normalized, direction.magnitude * 0.3f, Space.World); 76 } 77 }

  因為這個項目是PC,但是我是做手機游戲的,寫個什么程序都想在手機上跑一跑,因此有比較亂的平臺編譯宏,主要實現為獲得滑動的方向,就相當于在世界坐標下的xoy面的上的一個向量,求direction與z軸所成的面的法向量,求得的法向量就是本次旋轉的軸。再根據滑動的距離來設置相應的角度。項目傳到了github。有需要的小伙伴自取https://github.com/gaoxu1994/RotateForUnity

?

轉載于:https://www.cnblogs.com/gaoxu-1994/p/6600512.html

總結

以上是生活随笔為你收集整理的实现Unity编辑器模式下的旋转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。