Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by farrico · Jul 11, 2019 at 12:40 AM · 2d gameboxcollider2dcarsrunnerenable and disable script

Disabling and enabling again colliders

Hey guy, i was wandering if some one could help me to solve this problem i'm having when try to disable the BoxCollider2D the player (green marked car in img) has and after 1 second, enable it again. This is the script the player has attached:

 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 BoxCollider2D collider;
     
     
     void Start()
     {
         GameOverCanvas.SetActive (false);
         HudCanvas.SetActive (false);
         animator = GetComponent<Animator> ();   
         rigidbody = GetComponent<Rigidbody2D> ();
         collider = GetComponent<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);
 
         }
        
     }
 
  }   
 

[1]: https://pbs.twimg.com/media/D_Dr7sQXkAAqHWe.jpg:large

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by dbchest · Jul 11, 2019 at 01:06 AM

will you provide your question with descriptions of current and desired behavior for this script?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image farrico · Jul 11, 2019 at 01:13 AM 0
Share

Sorry, my bad :) There you go!

This is the img i reffer: https://pbs.twimg.com/media/D_Dr7sQXkAAqHWe.jpg:large

avatar image
0

Answer by I_Am_Err00r · Jul 11, 2019 at 01:21 AM

I already answered this in the post you had about this earlier, but I will just copy and paste real quick.

Add

 public float timeTillEnabled;

to your list of variables at the top of your script, copy this part of the script you already have:

 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;
           StartCoroutine(TurnOnCollider());
           rigidbody.isKinematic = true; 
           }
           
           }

Then add this block outside of any function; I typically put these after all other voids:

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

Set the public float timeTillEnabled to 1 in the inspector, and it will come back on in 1 second; alternitively you could hard code it in the script:

 yield return new WaitForSeconds(1f);

but setting it publicly in the inspector lets you change it later much easier

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

BoxCollider2D only registers clicks in top right quarter of its region 0 Answers

[2D Platformer] How Do Make My Character Walk Through A Platform 1 Answer

Weird 2d collision behaviour 1 Answer

2D collider troubles 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges