bobshellby
bobshellby

Reputation: 105

How do i make OnCollisionEnter function work?

So, i have a rocket which when is not colliding with the ground, it flies forward. When it hits something with the tag "Ground" it should stop and summon an explosion. However it is not detecting when it is touching "Ground" and goes through it.

I have tried changing how the collider works but it just made errors.

I added some print functions to see if it is actually triggered and it is not.

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

        }


    }
}

It should stop the rocket and then summon an explosion. however currently it goes through everything.

Sorry for pasting whole code. Any help would be greatly appreciated :3

Upvotes: 0

Views: 862

Answers (2)

Nikola G.
Nikola G.

Reputation: 335

If it is just passing by, then you need to see Tag, Collider and Rigidbody values. Be aware that this code below is for a 2D project, if you are working on a 3D project, make sure to apply change.

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

Upvotes: 1

Eric Bishop
Eric Bishop

Reputation: 529

First, make sure that you have a collider attached to your GameObject. If it is a 2D game, it should be a 2D collider, and the function needed to detect 2D collisions is OnCollisionEnter2D(), and make sure that the collider is not set to be a trigger in the inspector. If it is a 3D game, continue to use OnCollisionEnter(), and a 3D collider that is not set to be a trigger.

In case you actually want a trigger collider, you can use the functions OnTriggerEnter() and OnTriggerEnter2D() to detect collisions to a trigger collider. However, keep in mind that a trigger collider will allow stuff to go through it, but will still detect collisions.

Upvotes: 0

Related Questions