UNITY 旋转查看物体

PC端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using UnityEngine;
using System.Collections;
  
public class DragRound : MonoBehaviour {
  
    public Transform obj;
    public float speed = 2;
     
    private bool _mouseDown = false;
     
    void Update ()
    {
        if (Input.GetMouseButtonDown(0))
            _mouseDown = true;
        else if (Input.GetMouseButtonUp(0))
            _mouseDown = false;
 
        if (_mouseDown)
        {
            float fMouseX = Input.GetAxis("Mouse X");
            float fMouseY = Input.GetAxis("Mouse Y");
            obj.Rotate(Vector3.up, -fMouseX * speed, Space.World);
            obj.Rotate(Vector3.right, fMouseY * speed, Space.World);
        }
    }
}
  

手机端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using UnityEngine;
using System.Collections;
  
public class DragRoundPhone : MonoBehaviour {
  
    private Vector3 startFingerPos;
    private Vector3 nowFingerPos;
    private float xMoveDistance;
    private float yMoveDistance;
    private int backValue = 0;
    public GameObject obj;
    void Update()
    {
        if (Input.touchCount <= 0)
        {
            return;
        }
  
        if (Input.GetTouch(0).phase == TouchPhase.Began)
        {
            //Debug.Log("======开始触摸====="); 
            startFingerPos = Input.GetTouch(0).position;
        }
  
        nowFingerPos = Input.GetTouch(0).position;
  
        if ((Input.GetTouch(0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Ended))
        {
            startFingerPos = nowFingerPos;
            //Debug.Log("======释放触摸====="); 
            return;
        }
        //          if (Input.GetTouch(0).phase == TouchPhase.Ended) { 
        //               
        //          } 
        if (startFingerPos == nowFingerPos)
        {
            return;
        }
        xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);
        yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);
  
        if (xMoveDistance > yMoveDistance)
        {
            if (nowFingerPos.x - startFingerPos.x > 0)
            {
                //Debug.Log("=======沿着X轴负方向移动====="); 
                backValue = -1; //沿着X轴负方向移动 
            }
            else
            {
                //Debug.Log("=======沿着X轴正方向移动====="); 
                backValue = 1; //沿着X轴正方向移动 
            }
        }
        else
        {
            if (nowFingerPos.y - startFingerPos.y > 0)
            {
                //Debug.Log("=======沿着Y轴正方向移动====="); 
                backValue = 2; //沿着Y轴正方向移动 
            }
            else
            {
                //Debug.Log("=======沿着Y轴负方向移动====="); 
                backValue = -2; //沿着Y轴负方向移动 
            }
  
        }
        if (backValue == -1)
        {
            obj.transform.Rotate(Vector3.up * -1 * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == 1)
        {
            obj.transform.Rotate(Vector3.up * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == 2)
        {
            obj.transform.Rotate(Vector3.right * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == -2)
        {
            obj.transform.Rotate(Vector3.right * -1 * Time.deltaTime * 50, Space.World);
        
  
    
}

  

posted @   多见多闻  阅读(72)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示