首页 > 编程语言 > 详细

Unity3D判断触摸方向

时间:2017-02-24 19:31:54      阅读:263      评论:0      收藏:0      [点我收藏+]

据说

Temple Run(神庙逃亡)

就是用这种方式操作的

废话不多说

直接上代码

 

using UnityEngine;
using System.Collections;

public class Touch : MonoBehaviour
{
    private Vector2 beginPos;
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        TouchDirection();
    }

    void TouchDirection()
    {
        if (Input.touchCount <= 0)
            return;

        if (Input.touchCount == 1)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                beginPos = Input.touches[0].positon;
            }
            else if (Input.touches[0].phase == TouchPhase.Moved)
            {
            }

            if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
            {
                Vector2 pos = Input.touches[0].position;
                if (Mathf.Abs(beginPos.x = pos.x) > Mathf.Abs(beginPos.y = pos.y))
                {
                    if (beginPos.x > pos.x)
                    {
                        //向左
                    }
                    else
                    {
                        //向右
                    }
                }
                else
                {
                    if (beginPos.y > pos.y)
                    {
                        //向下
                    }
                    else
                    {
                        //向上
                    }
                }
            }
        }
    }
}

声明:此博客为个人学习之用,如与其他作品雷同,纯属巧合,并请明示指出

Unity3D判断触摸方向

原文:http://www.cnblogs.com/fws94/p/6439787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!