【问题标题】:How to fix movement to right and to left?如何固定向右和向左移动?
【发布时间】:2019-11-05 13:06:45
【问题描述】:

我花了很多时间试图找出代码中的问题,但我找不到我的代码没有被触发的解决方案。 在我之前的游戏中,当我实现此代码时它运行良好,现在当我在新游戏中实现相同的触摸移动代码时它不起作用。

我尝试调试代码并将 Debug.Log 放入 Update 方法中,当我在屏幕上滑动时它甚至没有触发。

这是代码:

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;
        }
    }
} 

我将 Debug.Log 放入此代码和 vector3.right 和 left 但它从未被触发。

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;
}

这是 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;
                }
            }
        }
    }
}

我调试的滑动输入的更新方法中的代码从未被调用或从未为我工作。我不明白我做错了什么,因为相同的代码实际上在我以前的游戏中工作。

非常感谢您阅读我的问题,我希望有人可以帮助我解决这个问题。

【问题讨论】:

  • 如果代码根本没有触发,您是否将脚本附加到您要移动的东西上?
  • 嗨。老兄,你完全改变了问题。你说它有效但总是向右移动,即使向左滑动也是如此。我找到了你的问题并回答了你为什么有这个错误以及如何修复它。但是现在您对其进行了编辑并提出了完全不同的问题,并且显然是完全不同的问题。我建议像以前一样恢复帖子并接受关闭帖子的答案,就像你应该在这个网站上做的那样。并为您的新问题创建新帖子。希望你能理解。

标签: c# unity3d game-development


【解决方案1】:

问题是你实际上对左右移动做同样的事情:

左:

desiredPos = transform.position + Vector3.left * 1.52f;

对:

desiredPos = transform.position - Vector3.right * 1.52f;

“+Vector3.left”和“-Vector3.right”一样,因为左边是(-1,0,0),右边是(1,0,0)。因此,在这两种情况下,您最终都会将相同的向量 (-1,0,0) 添加到您的位置,因为您在 Vector3.left 之前有加号以向左移动,而在 Vector3.right 之前有减号以向右移动。

所以你需要在两种情况下使用相同的符号(加号或减号),或者相同的向量(右或左)

【讨论】:

    猜你喜欢
    • 2013-08-13
    • 2012-11-26
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多