在我的 Unity 中,我使用 OnTriggerEnter2D 进行子弹和飞机的碰撞,但我在条件下输入的结果只发生一次

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

在下面的代码中,

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

public class Bullet : MonoBehaviour
{
    public Rigidbody2D rb;
    public int coins;
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        rb.velocity = new Vector2(0f, 8f);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Plane"))
        {
            coins += 5;
        }
    }
}

在 OnTriggerEnter2D 函数中,我比较了子弹和飞机,但硬币只增加了 5 一次。因此,如果子弹和飞机再次相撞,它仍然是 5,是的,我已经分配了标签。

我尝试将相同的结果应用于飞机和玩家等其他对象,但结果相同。

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