Advertisement

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    
    
 }
    
  
    
    
    
    
    cs
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/RhLKDI2JVQboFTPNuraxqXvHWefB.png)

全部评论 (0)

还没有任何评论哟~