zoukankan      html  css  js  c++  java
  • Unity 手指触摸的方向(单手)

    最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~

    using UnityEngine;
    using System.Collections;
    
    
    
    
    public class Demo : MonoBehaviour
    {
        public Vector3 lastMonseDown;
    
        /// <summary>
        /// 判断手指触摸的方向
        /// </summary>
        /// <returns></returns>
        TouchDir GetTouchDir()
        {
            //记录第一次手指点击的坐标点
            if (Input.GetMouseButtonDown(0))
            {
                lastMonseDown = Input.mousePosition;
            }
    
    
            //UICamera.hoveredObject防止NGUI点击穿透问题
            if (Input.GetMouseButtonUp(0) && UICamera.hoveredObject == null)
            {
                //结束坐标-开始坐标
                Vector3 mouseUp = Input.mousePosition;
                Vector3 touchOffset = mouseUp - lastMonseDown;
    
                //滑动超过50像素,算一次正确的滑动
                if (Mathf.Abs(touchOffset.x) > 50 || Mathf.Abs(touchOffset.y) > 50)
                {
                    if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x > 0)
                    {
                        return TouchDir.Right;
                    }
                    else if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x < 0)
                    {
                        return TouchDir.Left;
                    }
                    else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y > 0)
                    {
    
                        return TouchDir.Top;
                    }
                    else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y < 0)
                    {
    
                        return TouchDir.Bottom;
                    }
                }
                else
                {
                    return TouchDir.None;
                }
            }
    
            return TouchDir.None;
        }
    
    
    }
    
    
    /// <summary>
    /// 触摸的方向
    /// </summary>
    public enum TouchDir
    {
        None,
        Left,
        Right,
        Top,
        Bottom
    }
    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    安全购买数码相机的诀窍!(1)
    获得网卡MAC地址和IP地址
    用Asp.net实现基于XML的留言簿之二
    安全购买数码相机的诀窍!(2)
    使用Flash读取COOKIE
    数码常识:CCD的原理
    ACE 5.5 Make in RedHat AS 4 Update 4 Issue
    Eclipse Plugins 开发 (1)
    RedHat AS4 Update4 DNS (bind 9) 配置
    Maven2 & Continuum 持续整合 (1)
  • 原文地址:https://www.cnblogs.com/plateFace/p/4202316.html
Copyright © 2011-2022 走看看