我如何使OnCollisionEnter函数起作用?

时间:2019-04-07 21:39:31

标签: c# unity3d

所以,我有一枚火箭,当它不与地面碰撞时,它会向前飞行。当它碰到带有“地面”标签的东西时,它应该停止并召唤爆炸。但是,它没有检测到何时触摸“地面”并经过它。

我试图改变对撞机的工作方式,但它只是出错了。

我添加了一些打印功能,以查看它是否实际上被触发了。

using UnityEngine;
using System.Collections;

public class Rocket : MonoBehaviour
{
    //Public changable things
    public float speed = 20.0f;
    public float life = 5.0f;
    public bool canRunProgress = true;
    public bool isGrounded;
    public GameObject Explosion;
    public Transform rocket;
    public Rigidbody rb;


    // If the object is alive for more than 5 seconds it disapears.
    void Start()
    {
        Invoke("Kill", life);
    }


    //detects if tounching ground
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ground")
        {
            print("working");
            isGrounded = true;
            print("working");
        }
    }
    //detects if tounching ground
    void OnCollisionExit(Collision other)
    {
        if (other.gameObject.tag == "Ground")
        {
            isGrounded = false;
        }
    }

    //kill routine - explosion is summoned and explodes 2 seconds later it then destroys the rocket.
    IEnumerator Kill()
    {
        GameObject go = (GameObject)Instantiate(Explosion, transform); // also this needs to have the explosion be summoned in the middel of the rocket.
        yield return new WaitForSeconds(2f);
        Destroy(gameObject);

    }


    // Update is called once per frame
    void Update()
    {
        //if the object isn't tounching the ground and is able to run it's process
        if (isGrounded == false && canRunProgress)
        {
            transform.position += transform.forward * speed * Time.deltaTime;
            canRunProgress = true;
        }
        //if the object IS touching the ground it then makes the above process unable to work and then begins the kill routine
        else if(isGrounded == true)
        {
            print("YES");
            canRunProgress = false;
            StartCoroutine(Kill());

        }


    }
}

它应该停住火箭,然后召唤爆炸。但是目前它遍及一切。

很抱歉,粘贴整个代码。 任何帮助将不胜感激:3

2 个答案:

答案 0 :(得分:0)

首先,确保对您的GameObject附加了对撞机。如果是2D游戏,则应为2D对撞机,并且检测2D碰撞所需的功能为OnCollisionEnter2D(),并确保在检查器中未将对撞机设置为触发器。如果它是3D游戏,请继续使用OnCollisionEnter()和未设置为触发器的3D对撞机。

如果您确实需要触发器对撞机,则可以使用功能OnTriggerEnter()OnTriggerEnter2D()来检测与触发器对撞机的碰撞。但是,请记住,触发对撞机将允许物体通过它,但仍会检测到碰撞。

答案 1 :(得分:0)

如果它只是经过,那么您需要查看Tag,Collider和Rigidbody值。请注意,下面的代码适用于2D项目,如果您正在处理3D项目,请确保应用更改。

public class Example : MonoBehaviour
{
    public GameObject explosion;
    public bool isGrounded;

    private void Start()
    {
        Invoke("RemoveRocket", 5);
    }

    private void FixedUpdate()
    {
        if (!isGrounded)
        {
            MoveRocket();
        }
    }

    private void MoveRocket()
    {
        transform.position += (transform.right / .1f) * Time.deltaTime;
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            Instantiate(explosion, transform);
            isGrounded = true;
            CancelInvoke();
            Destroy(gameObject, 2);
        }
    }

    private void RemoveRocket()
    {
        Destroy(gameObject, 0);
    }
}