我是靠谱客的博主 畅快玫瑰,这篇文章主要介绍Unity3D学习-如何判断鼠标移动方向,现在分享给大家,希望可以做个参考。

1. 定义

public enum InputDirection{ 
    NULL,
    Right,
    Left,
    Down,
    Up

}

2. 属性

 InputDirection m_inputDir = InputDirection.NULL;
    bool activeInput = false;
    Vector3 m_mousePos;

3. 方法

void GetInputDirection() {
        m_inputDir = InputDirection.NULL;
        if (Input.GetMouseButtonDown(0)) {
            activeInput = true;
            m_mousePos = Input.mousePosition;
        
        }
        if (Input.GetMouseButton(0) && activeInput) {
            Vector3 Dir = Input.mousePosition - m_mousePos;
            if (Dir.magnitude > 20) {

                if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y)  & Dir.x > 0)
                {
                    m_inputDir = InputDirection.Right;
                }
                else if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y)   & Dir.x < 0)
                {
                    m_inputDir = InputDirection.Left;
                }
                else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) & Dir.y > 0)
                {
                    m_inputDir = InputDirection.Up;
                }
                else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y)  & Dir.y < 0)
                {
                    m_inputDir = InputDirection.Down;
                }
                activeInput = false;

            }
        
        }
        print(m_inputDir);
    }

最后

以上就是畅快玫瑰最近收集整理的关于Unity3D学习-如何判断鼠标移动方向的全部内容,更多相关Unity3D学习-如何判断鼠标移动方向内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(109)

评论列表共有 0 条评论

立即
投稿
返回
顶部