unity 手势输入识别方向

计算方向

 

向右

X的绝对值 > Y的绝对值,并且X > 0

 

向左

X的绝对值 > Y的绝对值,并且X < 0

 

向上

X的绝对值 < Y的绝对值,并且Y > 0

 

向下

X的绝对值 < Y的绝对值,并且Y < 0

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

public class PlayerMove : View
{
    #region 常量
    #endregion

    #region 事件
    #endregion

    #region 字段
    CharacterController m_cc;//角色控制器
    public float speed = 20;//速度
    InputDirection m_inputDir = InputDirection.NULL;//手势的方向
    bool activeInput = false;//按下激活状态
    Vector3 m_mousePos;//当前鼠标的位置
    #endregion

    #region 属性
    public override string Name { get { return Consts.V_PlayerMove; } }
    #endregion

    #region 方法
    //协程
    IEnumerator UpdateAction()
    {
        while (true)
        {
            m_cc.Move(transform.forward * speed * Time.deltaTime);//参数:向量*速度*时间间隔
            GetInputDirection();
            yield return 0;
        }
    }
    //获取手势输入
    void GetInputDirection()
    {
        //初始化方向为空
        m_inputDir = InputDirection.NULL;
        //如果鼠标按下
        if (Input.GetMouseButtonDown(0))
        {
            activeInput = true;
            m_mousePos = Input.mousePosition;//当前鼠标的位置
        }
        //如果鼠标按下,并且activeInput是真
        if (Input.GetMouseButton(0) && activeInput)
        {
            //方向 = 当前的位置 - 之前的位置
            Vector3 Dir = Input.mousePosition - m_mousePos;
            //Dir.magnitude返回向量的长度,如果长度大于20才进行手势判断
            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);
    }
    #endregion

    #region Unity回调
    private void Awake()
    {
        m_cc = GetComponent<CharacterController>();//获取主角的角色控制器
    }
    private void Start()
    {
        StartCoroutine(UpdateAction());//开户协程
    }
    #endregion

    #region 事件回调
    public override void HandleEvent(string name, object data)
    {

    }
    #endregion

    #region 帮助方法
    #endregion    
}

 

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值