• 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 bengoldzimer · Aug 01, 2017 at 02:33 PM · disableboxcollider2denabled

how to enable collider and after the hit to disable

hey every one i am trying to make a 2D game, i want that every time i press "Space" it will $$anonymous$$t with the weaponCollider.

its important that the collider will be disabled at the beginning and only when ill press the space bar it will be on and off after 0.5 sec or 1 sec...

t$$anonymous$$s is what i got on code, its not working good at all, i hope some1 can help me.

thank you, Ben.

 [SerializeField]
         public EdgeCollider2D WeaponCollider;
 -------------------------------------------------------------------------------------
     if (Input.GetKeyDown(KeyCode.Space))
             {
                 playerAnimator.Play("AttackAnimation");
                 playerAnimator.GetComponent<PlayerOneScript>().MeleeAttack();
             }
         }
     
         public void MeleeAttack()
         {
             WeaponCollider.enabled = !WeaponCollider.enabled;
         }
 



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
0

Answer by G3R1 · Aug 01, 2017 at 02:51 PM

 [SerializeField]
          public EdgeCollider2D WeaponCollider;
  -------------------------------------------------------------------------------------
      if (Input.GetKeyDown(KeyCode.Space))
              {
                  playerAnimator.Play("AttackAnimation");
                  playerAnimator.GetComponent<PlayerOneScript>().MeleeAttack();
              }
          }
      
          public void MeleeAttack()
          {
              WeaponCollider.enabled = true;
              StartCoroutine(DisableWeaponCollider());
           }
 
 
 private IEnumerator DisableWeaponCollider()
 {
        WeaponCollider.enabled = false;
        yield return WaitForSeconds(1.0f);
 }
 
 Did't understand what you needed exactly but I t$$anonymous$$nk t$$anonymous$$s will help you disable the collider after 1 second.
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 Rafit345 · Jan 07, 2022 at 05:53 PM 0
Share

I don't think this will work, this will probably enable the collider, then start the coroutine which will immediately turn the collider off and then wait one second after for no reason. It should first yield return new waitforseconds(1) and then disable the collider and stop itself.

avatar image
0

Answer by eazilec · Aug 01, 2017 at 08:02 PM

I'm working on a game that requires somet$$anonymous$$ng similar and t$$anonymous$$s is basically how I've done it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class answer : MonoBehaviour {
 
     public Animator playerAnimator;
 
     public EdgeCollider2D weaponCollider;
 
     //T$$anonymous$$s is true w$$anonymous$$le collider is ENABLED and player is attacking. 
     //We'll check for t$$anonymous$$s when checking for input to make sure player isn't already attacking.
     private bool attacking;
 
     //T$$anonymous$$s is the duration of the attack and how long the collider will be ENABLED for. 
     //I set its default value to 0.5, but it can be 1 or anyt$$anonymous$$ng else.
     public float attackDuration = 0.5f;
 
     //T$$anonymous$$s is the exact time in seconds since the game started that the attack will end and the collider will be DISABLED. 
     //We'll calculate t$$anonymous$$s by adding the "attackDuration" to the exact game time in seconds when the attack began.
     private float timeToAttackEnd; 
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         //Check for input and if player is attacking.
         if(Input.GetKeyDown(KeyCode.Space) && !attacking)
         {
             playerAnimator.Play("AttackAnimation");
 
             attacking = true;
 
             //Set the exact time that attack will end to be the TIME the attack is called PLUS the DURATION of the attack we specified earlier.
             timeToAttackEnd = Time.time + attackDuration;
         }
 
         //Check for attacking
         if(attacking)
         {
             //T$$anonymous$$s will be called every frame w$$anonymous$$le attacking is true so we can check that the TIME to end the attack has been reached.
             MeleeAttack();
         }
         
     }
 
     public void MeleeAttack()
     {
         //If the TIME to end the attack has been reached, DISABLE the collider 
         //and set "attacking" to false so t$$anonymous$$s function will no longer be called and we can attack again if we choose.
         if(Time.time > timeToAttackEnd)
         {
             weaponCollider.enabled = false;
             attacking = false;
         }
 
         //every frame BEFORE the TIME to end the attack is reached, the collider will be ENABLED.
         else
         {
             weaponCollider.enabled = true;
         }
 
     }
 }


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 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

Related Questions

How to make an open and close box?,How to make open and close a box? 0 Answers

Disabling THIS script from code 1 Answer

How to : enable/disable OnGUI() script on C# 1 Answer

Enabling/Disabling Multiple Tagged objects 1 Answer

Information about Behaviour.enabled 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