Problems with collisions

Hey guys!

Im trying to do an “infinite runner”.

Came to the instance where i need to disable my Player (green mark in img) Collision for about 1 second and then make it active again.

Could anyone please help?

This is what i’m using:

    void Update()
    {
        if (Input.GetKeyDown (jump))
        {
        animator.SetTrigger ("Boost");
        GetComponent<AudioSource>().Play ();
        GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
        HudCanvas.SetActive (true);
        
        rigidbody.isKinematic = true; 
        }
        
        }

alt text

I’m assuming you are using a polygoncollider there and that this script is attached to the actual object that collider is attached to.

void Update()
         {
             if (Input.GetKeyDown (jump))
             {
             animator.SetTrigger ("Boost");
             GetComponent<AudioSource>().Play ();
             GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
             HudCanvas.SetActive (true);
             GetComponent<PolygonCollider2D>().enabled = false
             rigidbody.isKinematic = true; 
             }
             
             }

Sorry, but when i do what you say, a new buch of errors appears (img)

The Whole code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using Spine.Unity;

public class CarControllerBis : MonoBehaviour

{
    public Animator animator;
    public KeyCode teclas;
    public Rigidbody2D rigidbody;
    public GameObject GameOverCanvas;
    public GameObject HudCanvas;
    public GameObject Gas;
    public float startTime;
    public float waitInSeconds = 1.0F;
    
    void Start()
    {
        GameOverCanvas.SetActive (false);
        HudCanvas.SetActive (false);
        animator = GetComponent<Animator> ();   
        rigidbody = GetComponent<Rigidbody2D> ();
        startTime = Time.time;
        collider = GetComponenet<BoxCollider2D>();
        
    }

    void Update()
    {
         if (Input.GetKeyDown (teclas))
         {
         animator.SetTrigger ("Boost");
         GetComponent<AudioSource>().Play ();
         GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
         HudCanvas.SetActive (true);
         
         collider.enabled = false;
         
         rigidbody.isKinematic = true; 
         }
         
         }
        
        
        
    void OnCollisionEnter2D (Collision2D collision)
   {   
        if (collision.gameObject.tag == "Hazard")
        {
        HudCanvas.GetComponent<Animator>().SetTrigger("HudOut");
        SoundManagerScript.PlaySound ("Hit");
        animator.SetTrigger ("Hit");
        Destroy(this.gameObject.GetComponent<Rigidbody2D>()); 
        Destroy(this.gameObject.GetComponent<CarControllerBis>());
        
        GameOverCanvas.SetActive (true);
        DestroyObject (Gas);

        }
       
    }

 }

wherever you need to add the collider back on, you just add this line of code:

collider.enabled = true;

if you want to have the collider back on in a specified amount of time, first you would need to add a public float value and name it something like timeTillEnabled, then paste this line of code right after the collider.enabled = false; line:

StartCoroutine(TurnOnCollider());

Then put this function somewhere outside of another function; I usually put IEnumerators at the bottom of my scripts:

        IEnumerator TurnOnCollider()
        {
            yield return new WaitForSeconds(timeTillEnabled);
            collider.enabled = true;
            yield break;
        }

In the inspector just remember to set the value for timeTillEnabled, and that should just turn back on when that time is up.