仍然发生碰撞时调用OnTriggerExit2D

时间:2019-06-24 19:40:42

标签: c# unity3d

我正在创建2D自上而下的游戏。我希望玩家走到平台上并使平台在其下方移动以克服障碍。

为此,我创建了一个平台,可以在位置之间来回移动。如果玩家与平台碰撞,他们会将其transform.parent设置为平台transform

这有效,并且播放器随平台一起移动。问题在于,一旦平台移过原始BoxCollider2D所在的位置,它就会调用`OnTriggerExit2D。即使播放器仍在与平台碰撞,也将调用此方法。唯一的区别是它现在是它的子代。

[RequireComponent(typeof(Rigidbody2D))]
public class MovingPlatform : MonoBehaviour
{
    public List<Vector2> positions;
    public float speed = 1;
    public bool forward = true;
    public float stopTime = 1;

    private Vector2 nextPosition;
    private Rigidbody2D rigidbody;

    private int currentPosition = 0;
    private float waitTime = 0;

    private GameObject collidingObject;

    // Start is called before the first frame update
    void Start()
    {
        nextPosition = positions[0];
        rigidbody = GetComponent<Rigidbody2D>();
    }

    /**
     * Code to move the platform back and forth between positions
     */
    void Update()
    {
        if (waitTime <= 0)
        {
            if (Vector3.Distance(transform.position, nextPosition) > 0)
            {
                float step = speed * Time.deltaTime;
                transform.position = Vector3.MoveTowards(transform.position, nextPosition, step);
            }
            else
            {
                if (forward)
                {
                    if (currentPosition < positions.Count - 1)
                    {
                        currentPosition++;
                        nextPosition = positions[currentPosition];
                    }
                    else
                    {
                        waitTime = stopTime;
                        forward = false;
                    }
                }
                else
                {
                    if (currentPosition > 0)
                    {
                        currentPosition--;
                        nextPosition = positions[currentPosition];
                    }
                    else
                    {
                        waitTime = stopTime;
                        forward = true;
                    }
                }
            }
        }
        else
        {
            waitTime -= Time.deltaTime;
        }

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag.Equals("Player"))
        {
            Debug.Log("Connecting");
            collidingObject = other.gameObject;
            collidingObject.transform.parent = transform;
        }

    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag.Equals("Player") && collidingObject != null)
        {
            Debug.Log("Disconnecting");
            collidingObject.transform.parent = null;
            collidingObject = null;
        }
    }
}

似乎正在检测与平台过去的位置的碰撞,并且没有更新刚体的新位置。

可能是什么原因造成的?

更新 我尝试在Player组件中使用它。

private void OnTriggerStay2D(Collider2D other)
{
    if (other.tag.Equals("Moving Platform"))
    {
        Debug.Log("Connecting");
        transform.parent = other.transform;
    }
    else
    {
        Debug.Log("Disconnecting");
        transform.parent = null;
    }
}

它已成功添加移动平台作为其子平台,但仍会在碰撞时将父平台设置为null。

0 个答案:

没有答案