您现在的位置是:首页 >学无止境 >unity 鼠标旋转物体网站首页学无止境

unity 鼠标旋转物体

weixin_42399500 2025-03-27 12:01:02
简介unity 鼠标旋转物体

TouchController

    public class TouchController : MonoBehaviour
    {
        public GameObject Cube;
        public float Speed = 0.1f;
    
        void Update ()
        {
            #if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)

        //Touch
        int touchCount = Input.touchCount;
        
        if (touchCount == 1)
        {
            
            Touch t = Input.GetTouch(0);
            if(EventSystem.current.IsPointerOverGameObject(t.fingerId))return;
            
            switch (t.phase)
            {
            case TouchPhase.Moved:
                
                float xAngle = t.deltaPosition.y * Speed;
                float yAngle = -t.deltaPosition.x * Speed;
                float zAngle = 0;
                
                Cube.transform.Rotate(xAngle, yAngle, zAngle, Space.World);
                
                break;
            }
            
        }

            #else
            //Mouse
            if (Input.GetMouseButton (0)) {
                if (EventSystem.current.IsPointerOverGameObject ())
                    return;
            
                float xAngle = Input.GetAxis ("Mouse Y") * Speed * 80;
                float yAngle = -Input.GetAxis ("Mouse X") * Speed * 80;
                float zAngle = 0;
            
                Cube.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
            }
            #endif
        }
    }

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。