【问题标题】:Why doesn't "OnTriggerEnter2D()" work when two specific objects collide?为什么当两个特定对象发生碰撞时“OnTriggerEnter2D()”不起作用?
【发布时间】:2021-01-14 03:43:53
【问题描述】:

我正在用 C# 统一创建这款飞扬小鸟风格的游戏。

我在游戏控制器脚本中有一个评分函数。在这里……

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

其实鸟和飞机是一回事。

当飞机与星星重叠时,我想做的是让鸟得分/运行 BirdScored() 函数。飞机有一个 Rigidbody2D 和一个对撞机,星星有一个 Rigidbody2D 但没有对撞机,因为在鸟的脚本中,如果飞机发生碰撞,它就会摧毁。

这是鸟脚本

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

这里是星星脚本...

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

出了什么问题,我该怎么办?

【问题讨论】:

  • 明星需要一个collider2d
  • 你也调试过这个吗?是根本没有调用该方法,还是您的 GameControler 中的某些内容不起作用?

标签: c# unity3d triggers overlap


【解决方案1】:

在星星上放置一个 Colider2D,并检查检查器中的Is Trigger 选项。

Is Trigger 禁用与任何其他 collider2d 的碰撞,因此您的飞机不会被OnCollisionEnter2D 摧毁,但OnTriggerEnter2D 将正确触发。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2017-08-16
      • 2016-01-13
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多