简体   繁体   English

如何通过向右和向左滑动来修复运动?

[英]How to fix movement with swipe to right and left?

I lost a lot of time trying to find what is the problem in code but I can not find the solution why my code is not triggered. 我失去了很多时间试图找到代码中的问题,但我找不到解决方案为什么我的代码没有被触发。 In my previous game when I implemented this code it worked perfectly, now when i implement into new game this same code for touch movement it doesn't work. 在我之前的游戏中,当我实现这个代码时,它工作得很好,现在当我在新游戏中实现这个相同的触摸移动代码时,它不起作用。

I tried to debug the code and put Debug.Log into Update method and when i swipe over screen it doesn't even get trigger. 我试图调试代码并将Debug.Log放入Update方法,当我在屏幕上滑动时,它甚至没有触发。

This is the code: 这是代码:

int left = 0;
int right = 0;
int maxLeftCycles = 5;
int maxRightCycles = 5;

void Start()
{
    //touch
    left = maxLeftCycles;
    right = maxRightCycles;
}

private void Update()
{
    timer += Time.deltaTime;

    if (Input.GetKeyUp(KeyCode.RightArrow) ||
        Swipe.swipe == Swipe.SwipeDirection.right)
    {
        Swipe.ResetSwipe();
        right = 0;
    }

    if (Input.GetKeyUp(KeyCode.LeftArrow) ||
        Swipe.swipe == Swipe.SwipeDirection.left)
    {
        Swipe.ResetSwipe();
        left = 0;
    }
}

void FixedUpdate()
{
    if (left < maxLeftCycles && !isMoving)
    {
        desiredPos = transform.position + Vector3.left * 1.52f;
        isMoving = true;

        left++;
    }

    if (right < maxRightCycles && !isMoving)
    {
        desiredPos = transform.position - Vector3.right * 1.52f;
        isMoving = true;

        right++;
    }

    if (isMoving)
    {
        transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);

        // this == is true if the difference between both
        // vectors is smaller than 0.00001
        if (transform.position == desiredPos)
        {
            isMoving = false;

            transform.position = desiredPos;
        }
    }
} 

I put Debug.Log in this code and in vector3.right and left but it never get triggered. 我把Debug.Log放在这个代码中,在vector3.right中,但是它永远不会被触发。

if (Input.GetKeyUp(KeyCode.RightArrow) ||
    Swipe.swipe == Swipe.SwipeDirection.right)
{
    Debug.Log("This is traacked");

    Swipe.ResetSwipe();
    right = 0;
}

if (Input.GetKeyUp(KeyCode.LeftArrow) ||
    Swipe.swipe == Swipe.SwipeDirection.left)
{
    Debug.Log("This is traacked");

    Swipe.ResetSwipe();
    left = 0;
}

This is the code for Swipe script 这是Swipe脚本的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Swipe : MonoBehaviour
{
    private float fingerStartTime = 0.0f;
    private Vector2 fingerStartPos = Vector2.zero;

    private bool isSwipe = false;
    private float minSwipeDist = 50.0f;
    private float maxSwipeTime = 0.5f;

    public enum SwipeDirection
    {
        none,
        up,
        down,
        right,
        left
    }

    public static SwipeDirection swipe;

    void Start()
    {
        swipe = SwipeDirection.none;
    }

    public static void ResetSwipe()
    {
        swipe = SwipeDirection.none;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            foreach (Touch touch in Input.touches)
            {
                switch (touch.phase)
                {
                    case TouchPhase.Began:
                        /* this is a new touch */
                        isSwipe = true;
                        fingerStartTime = Time.time;
                        fingerStartPos = touch.position;
                        break;

                    case TouchPhase.Canceled:
                        /* The touch is being canceled */
                        isSwipe = false;
                        break;

                    case TouchPhase.Ended:

                        float gestureTime = Time.time - fingerStartTime;
                        float gestureDist = (touch.position - fingerStartPos).magnitude;

                        if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
                        {
                            Vector2 direction = touch.position - fingerStartPos;
                            Vector2 swipeType = Vector2.zero;

                            if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
                            {
                                // the swipe is horizontal:
                                swipeType = Vector2.right * Mathf.Sign(direction.x);
                            }
                            else
                            {
                                // the swipe is vertical:
                                swipeType = Vector2.up * Mathf.Sign(direction.y);
                            }

                            if (swipeType.x != 0.0f)
                            {
                                if (swipeType.x > 0.0f)
                                {
                                    // MOVE RIGHT
                                    swipe = SwipeDirection.right;
                                }
                                else
                                {
                                    // MOVE LEFT
                                    swipe = SwipeDirection.left;
                                }
                            }

                            if (swipeType.y != 0.0f)
                            {
                                if (swipeType.y > 0.0f)
                                {
                                    // MOVE UP
                                    swipe = SwipeDirection.up;
                                }
                                else
                                {
                                    // MOVE DOWN
                                    swipe = SwipeDirection.down;
                                }
                            }
                        }
                        break;
                }
            }
        }
    }
}

The code in Update method for swipe input which I debug never get called or never work for me.I can not understand what i am doing wrong because the same code actually works in my previous game. 我调试的刷卡输入的Update方法中的代码永远不会被调用或从不为我工作。我无法理解我做错了什么,因为相同的代码实际上在我之前的游戏中有效。

Thank you so much for reading my question I hope there will be some guy who can help me to solve this issue. 非常感谢你阅读我的问题,我希望有一些人可以帮助我解决这个问题。

If it worked as is before hand in another project I would make sure that you are attaching the script to a game object. 如果它在另一个项目中按原样运行,我会确保您将脚本附加到游戏对象。 If it is attached to a game object make sure that it is not marked as inactive and that you aren't turning the object to inactive somewhere else in your scripts. 如果它附加到游戏对象,请确保它未被标记为非活动状态,并且您没有将对象转换为脚本中其他位置的非活动状态。

If neither of these are the case I would also try removing the script from the object and then reattaching it, and if that still doesn't work try deleting the object the script is attached to (if possible) and then recreate it and reattach the script. 如果这两种情况都不是这样,我也会尝试从对象中删除脚本,然后重新连接它,如果仍然无效,请尝试删除脚本所附加的对象(如果可能),然后重新创建它并重新连接脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM