在滑动Unity上控制Gameobject

时间:2018-07-11 09:15:10

标签: unity3d controls swipe

我刚开始学习团结并开发了https://play.google.com/store/apps/details?id=com.appadvisory.booooing&hl=en之类的游戏。我想像游戏中的球一样滑动来控制我的游戏对象。我应该如何编写代码?,Im biggner团结一致地开发出像https://play.google.com/store/apps/details?id=com.appadvisory.booooing&hl=en这样的游戏。我想像提供的链接中的球一样滑动来控制我的游戏对象。代码是什么?

1 个答案:

答案 0 :(得分:0)

以下脚本将检测所有方向的滑动。

将逻辑放在以下功能中

OnSwipeLeft() 
OnSwipeRight() 
OnSwipeTop() 
OnSwipeBottom()

此脚本将仅检测屏幕上的滑动,

根据滑动方向,向球添加力,速度或平移


using UnityEngine;
using System.Collections;

public class SwipeDetector : MonoBehaviour {

    private const int mMessageWidth  = 200;
    private const int mMessageHeight = 64;

    private readonly Vector2 mXAxis = new Vector2(1, 0);
    private readonly Vector2 mYAxis = new Vector2(0, 1);

    private readonly string [] mMessage = {
        "",
        "Swipe Left",
        "Swipe Right",
        "Swipe Top",
        "Swipe Bottom"
    };

    private int mMessageIndex = 0;

    // The angle range for detecting swipe
    private const float mAngleRange = 30;

    // To recognize as swipe user should at lease swipe for this many pixels
    private const float mMinSwipeDist = 50.0f;

    // To recognize as a swipe the velocity of the swipe
    // should be at least mMinVelocity
    // Reduce or increase to control the swipe speed
    private const float mMinVelocity  = 2000.0f;

    private Vector2 mStartPosition;
    private float mSwipeStartTime;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {

        // Mouse button down, possible chance for a swipe
        if (Input.GetMouseButtonDown(0)) {
            // Record start time and position
            mStartPosition = new Vector2(Input.mousePosition.x,
                                         Input.mousePosition.y);
            mSwipeStartTime = Time.time;
        }

        // Mouse button up, possible chance for a swipe
        if (Input.GetMouseButtonUp(0)) {
            float deltaTime = Time.time - mSwipeStartTime;

            Vector2 endPosition  = new Vector2(Input.mousePosition.x,
                                               Input.mousePosition.y);
            Vector2 swipeVector = endPosition - mStartPosition;

            float velocity = swipeVector.magnitude/deltaTime;

            if (velocity > mMinVelocity &&
                swipeVector.magnitude > mMinSwipeDist) {
                // if the swipe has enough velocity and enough distance

                swipeVector.Normalize();

                float angleOfSwipe = Vector2.Dot(swipeVector, mXAxis);
                angleOfSwipe = Mathf.Acos(angleOfSwipe) * Mathf.Rad2Deg;

                // Detect left and right swipe
                if (angleOfSwipe < mAngleRange) {
                    OnSwipeRight();
                } else if ((180.0f - angleOfSwipe) < mAngleRange) {
                    OnSwipeLeft();
                } else {
                    // Detect top and bottom swipe
                    angleOfSwipe = Vector2.Dot(swipeVector, mYAxis);
                    angleOfSwipe = Mathf.Acos(angleOfSwipe) * Mathf.Rad2Deg;
                    if (angleOfSwipe < mAngleRange) {
                        OnSwipeTop();
                    } else if ((180.0f - angleOfSwipe) < mAngleRange) {
                        OnSwipeBottom();
                    } else {
                        mMessageIndex = 0;
                    }
                }
            }
        }
    }

    void OnGUI() {
        // Display the appropriate message
        GUI.Label(new Rect((Screen.width-mMessageWidth)/2,
                           (Screen.height-mMessageHeight)/2,
                            mMessageWidth, mMessageHeight),
                  mMessage[mMessageIndex]);
    }

    private void OnSwipeLeft() {
        mMessageIndex = 1;
    }

    private void OnSwipeRight() {
        mMessageIndex = 2;
    }

    private void OnSwipeTop() {
        mMessageIndex = 3;
    }

    private void OnSwipeBottom() {
        mMessageIndex = 4;
    }
}

有关更多信息,请参阅此Detect Swipe