码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D判断触摸方向

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

标签:start   direction   cto   pre   else   npos   use   mono   end   

据说

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判断触摸方向

标签:start   direction   cto   pre   else   npos   use   mono   end   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!