Search Unity

Disable a 2D collider after certain time.

Discussion in '2D' started by farrico, Oct 5, 2019.

  1. farrico

    farrico

    Joined:
    Feb 8, 2017
    Posts:
    20
    Hi everyone.
    I'm new programing Games.
    I'm making a simple 2D platform... i have an unstable platform wich should disable its 2D collider after one second the player steps in.

    The video shows the example to what i've achieved so far.


    Code (CSharp):
    1. public class BaseUnstable : MonoBehaviour
    2. {
    3.     public Animator animator;
    4.  
    5.     void Start()
    6.     {
    7.         animator = GetComponent<Animator> ();  
    8.     }
    9.  
    10.     void OnCollisionEnter2D (Collision2D collision)
    11.     {  
    12.         if (collision.gameObject.tag == "Player")
    13.         {
    14.             animator.SetTrigger ("Boost");
    15.             GetComponent<AudioSource>().Play ();
    16.         }
    17.     }
    18. }


    Can any one please help?

     
  2. Nivbot

    Nivbot

    Joined:
    Mar 21, 2015
    Posts:
    65
    There are quite a few way to do it. You can just put the collider on that part of the bridge and then when the timer is up.. or the animation (you can use an animation event at the end of the animation if it is an animation) and just put Destroy(bridge_gameObject);
    You can also, make the bridge a prefab, then instantiate it and destroy the instantiated one. Which is what I would personally do. Or, you can Keep the bridge there, add a child object to the bridge that has the collider and destroy the child object.
    That's just thinking off the top of my head. You can also just make the collider a trigger when the bridge falls. Triggers don't cause collisions.
     
    farrico and vakabaka like this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can try invoke (not sure about spelling)
    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Player")
    4.         {
    5.             animator.SetTrigger ("Boost");
    6.             GetComponent<AudioSource>().Play ();
    7. Invoke ("DisableCollider", 1f);
    8.         }
    9.     }
    10.  
    11. void  DisableCollider () {
    12. //GetComponent<Collider2D>().enabled = false;
    13. //or something like Nivbot has said
    14. }
    or maybe with coroutine:
    Code (CSharp):
    1. //..........
    2. StartCoroutine ("DisableCollider");
    3.         }
    4.     }
    5.  
    6. IEnumerator DisableCollider () {
    7. yield return new WaitForSeconds (1f);
    8. //GetComponent<Collider2D>().enabled = false;
    9. }
     
    Nivbot and farrico like this.
  4. farrico

    farrico

    Joined:
    Feb 8, 2017
    Posts:
    20
    Thank you so much, once ive tried to add a child with the collider, but i cold'nt figure out how to reference (i'm a noob, i know :D)
     
  5. farrico

    farrico

    Joined:
    Feb 8, 2017
    Posts:
    20

    It worked!!!! THANK YOU SO MUCH!!!!!
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can declare a gameobject in the script (later drag&drop the childobject in the inspector to initialize it):
    public GameObject childObject;

    then disable it when this is needed:
    childObject.SetActive (false);
     
    Nivbot likes this.
  7. Nivbot

    Nivbot

    Joined:
    Mar 21, 2015
    Posts:
    65
    I agree with Vakabaka, however, I would make a void and make the collider do whatever you want in an animation event. That way it is timed perfectly with the animation. But that's just nitpicking