首页 > 解决方案 > Why doesn't "OnTriggerEnter2D()" work when two specific objects collide?

问题描述

I am creating this flappy bird style game in unity with C#.

My current progress

I have a scored function in the Game Controller script. Here it is...

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

public class GameController : MonoBehaviour
{
    private int score = 0;
    public float starScrollSpeed;
    public float groundScrollSpeed;
    public float skyScrollSpeed;
    public GameObject gameOverText;
    public GameObject playAgain;
    public bool gameOver = false;
    public static GameController instance;
    public Text scoreText;

    // Start is called before the first frame update
    void Awake()
    {
        if(instance == null)
        {
            instance = this;
        }

        else if(instance != this)
        {
            Destroy(gameObject);
        }
    }

    // Update is called once per frame

     void Start()
    {
        
    }
    
    void Update()
    {
        
    }

    public void BirdScored()
    {
        if (gameOver)
        {
            return;
        }
        score++;
        scoreText.text = "SCORE  " + score.ToString();
    }

    public void PlaneDestroyed()
    {
        gameOverText.SetActive(true);
        playAgain.SetActive(true);
        gameOver = true;
    }
}

Actually Bird and Plane is the same thing.

What I want to do is to make the bird score/run the BirdScored() function when the Plane overlaps with a star. The Plane has a Rigidbody2D and a collider and stars have a Rigidbody2D but no collider because In the bird script if the plane collide, it destroys.

Here is the Bird Script

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

public class Bird : MonoBehaviour
{
    private bool isDead = false;
    private Rigidbody2D rb2d;
    public float upforce = 200f;
    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

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

        if (isDead == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                rb2d.velocity = Vector2.zero;
                rb2d.AddForce(new Vector2(0, upforce));
            }
        }

        anim.SetTrigger("Flap");
    }

    void OnCollisionEnter2D()
    {
        
        isDead = true;
        anim.SetTrigger("Die");
        GameController.instance.PlaneDestroyed();
    }
}

And here is the star script...

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

public class Stars : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.name == "Plane")
        {
            GameController.instance.BirdScored();
        }
    }
}

What is wrong and what should I do?

标签: c#unity3dtriggersoverlap

解决方案


我可以在您的屏幕截图中看到对撞机未设置为“触发”,这使得它无法注册触发碰撞。


推荐阅读