为什么我的元素在碰到东西之前不会移动?团结

问题描述 投票:0回答:0

我想让我的元素移动,直到它在我滑动时碰到某物。

我得到以下代码:

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

public class PlayerController : MonoBehaviour
{
        IEnumerator OnSwipeUp()
    {
        if(!isCurrentlyColliding)
        {
            transform.Translate(0,0.5f,0);
            yield return null;
        }else{
            StopCoroutine(OnSwipeUp());
        }
        
    }

    IEnumerator OnSwipeDown()
    {
        if(!isCurrentlyColliding)
        {
            transform.Translate(0,-0.5f,0);
            yield return null;
        }else{
            StopCoroutine(OnSwipeUp());
        }
    }

    IEnumerator OnSwipeLeft()
    {
        if(!isCurrentlyColliding)
        {
            transform.Translate(-0.5f,0,0);
            yield return null;
        }else{
            StopCoroutine(OnSwipeUp());
        }
    }

    IEnumerator OnSwipeRight()
    {
        if(!isCurrentlyColliding)
        {
            transform.Translate(0.5f,0,0);
            yield return null;
        }else{
            StopCoroutine(OnSwipeUp());
        }
    }
    bool isCurrentlyColliding = false;
    void OnCollisionEnter2D(Collision2D col) {
        isCurrentlyColliding = true;
    }
 
    void OnCollisionExit2D(Collision2D col) {
        isCurrentlyColliding = false;
    }

        float verticalMove()
    {
        return Mathf.Abs(fingerDown.y - fingerUp.y);
    }

    float horizontalValMove()
    {
        return Mathf.Abs(fingerDown.x - fingerUp.x);
    }
    private Vector2 fingerDown;
    private Vector2 fingerUp;

    public float SWIPE_THRESHOLD = 20f;

    void checkSwipe()
    {
        //Check if Vertical swipe
        if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove())
        {
            //Debug.Log("Vertical");
            if (fingerDown.y - fingerUp.y > 0)//up swipe
            {
                StartCoroutine(OnSwipeUp());
            }
            else if (fingerDown.y - fingerUp.y < 0)//Down swipe
            {
                StartCoroutine(OnSwipeDown());
            }
            fingerUp = fingerDown;
        }

        //Check if Horizontal swipe
        else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
        {
            //Debug.Log("Horizontal");
            if (fingerDown.x - fingerUp.x > 0)//Right swipe
            {
                StartCoroutine(OnSwipeRight());
            }
            else if (fingerDown.x - fingerUp.x < 0)//Left swipe
            {
                StartCoroutine(OnSwipeLeft());
            }
            fingerUp = fingerDown;
        }

        //No Movement at-all
        else
        {
            //Debug.Log("No Swipe!");
        }
    }


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

        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fingerUp = touch.position;
                fingerDown = touch.position;
            }

            if (touch.phase == TouchPhase.Ended)
            {
                fingerDown = touch.position;
                checkSwipe();
            }
        }
    }
}

但它只移动一个“场”,如果它与某物发生碰撞也不会停止。我的播放器有一个 2D Box Collider 和我的墙。我需要刚体还是什么?我使用协程,所以它可以检查它是否发生碰撞。在我有 while 循环阻止代码进一步执行之前。如果你能帮助我,我会很高兴。

unity3d 2d collision-detection
© www.soinside.com 2019 - 2024. All rights reserved.