Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pur3 · Jul 19, 2016 at 03:29 AM · c#2d2d-platformerboxcollider2d

How to turn off a collider when I activate it (Basic Sword mechanic) [2D]

Hey guys,

I am looking to turn off the box collider 2D of my characters weapon. Currently I have it to where: When the player left clicks, it will activate the box collider and when the player releases the left click, it turns off the box collider. I want it to where it activates it on left click and automatically turns it back off no matter what. I do have a cool down system to where the player can't click for another second, but as of now the player can just hold down the mouse key to simply avoid the cool down.

This is all within the same script: PlayerSM

 //Where player activates mouse click
 void MeleeHandler () 
     {
         if (Input.GetKeyDown (KeyCode.Mouse0) && meleeCDbool == false) 
         {
             MeleeBox.SetActive (true);
             meleeCDbool = true;
         }
 
         if (Input.GetKeyUp (KeyCode.Mouse0)) 
         {
             MeleeBox.SetActive (false);
         }
     }
 ---------------------------------------------------------------------------------------------------------
 //Cooldown for the attack
 
 void Timers() 
     {
         if (meleeCDbool == true) 
         {
             meleeCD -= Time.deltaTime;
 
             if (meleeCD <= 0) 
             {
                 meleeCDbool = false;
                 meleeCD = MaxMeleeCD;
             }
         }
     }

Comment
Add comment · Show 7
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 Glurth · Jul 19, 2016 at 05:03 AM 0
Share

Perhaps you will have better luck using the mouse functions like: https://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$ouseButtonDown.html

avatar image Pur3 Glurth · Jul 19, 2016 at 07:18 PM 0
Share

Thanks for commenting!

What would be the difference for the mouse keys on how I am doing it here compared to in the link you posted? Wouldn't it generally be the same thing, just differently written?

avatar image Glurth Pur3 · Jul 19, 2016 at 07:24 PM 0
Share

Honestly don't know. I would too would assume NO difference, but assumptions can be mistaken. It's just a shot in the dark, cuz the logic looks good to me.

Show more comments
avatar image JoshDangIt · Jul 20, 2016 at 03:05 AM 0
Share

$$anonymous$$aybe I don't understand your question, but if you only want the box collider to be active for a second after you click, why not just make another timer that will disable the collider after it runs out?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by OutOfRam · Jul 20, 2016 at 04:41 AM

My advice would be to use a co-routine here. If I understand you correctly when you click, you want the the collier to turn on then after x amount of time turn off.

  private IEnumerator MeleeHandler ()
  {
      if (Input.GetKeyDown (KeyCode.Mouse0) && meleeCDbool == false) 
      {
          MeleeBox.SetActive (true);
          meleeCDbool = true;
          yield return new WaitForSeconds(1.0f); // whatever time you want between on and off in seconds put in brackets so 1.0 would be on for 1 second then turn off 0.1 would be a tenth of a second
           MeleeBox.SetActive(false);
      }
      yield return null;
  }

This should make the collier turn off regardless of whether they hold the button or not. Just remember to make sure that you initiate the co-routine where ever you do as:

 StartCoroutine("MeleeHandler"); // Rather than MeleeHandler()
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 Pur3 · Jul 20, 2016 at 10:14 PM 0
Share

Hey thanks for commenting!

THIS is what I have been looking for, I don't know why I never thought of having a Coroutine! I even have some in my code from earlier, wow. Once again, thank you for this answer, I have been stuck on this for 2 days now lol

Thanks!

avatar image
0

Answer by rmassanet · Jul 19, 2016 at 06:58 AM

Colliders are components, not GameObjects. Therefore, assuming MeleeBox is a Collider it should be enabled and disabled like this: MeleeBox.enabled = false; Additionally, I am assuming you are calling MeleeHandlers and Timers in your Update function. If you do that, your code should work.

Comment
Add comment · Show 2 · 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 Pur3 · Jul 19, 2016 at 07:17 PM 0
Share

Thanks for commenting!

I basically created an empty game object that has a collider on it, so it is a GameObject, I should have clarified that earlier. I am calling Timers and $$anonymous$$eleeHandlers in my update function but I want to fix how the player can 'cheese' the system.

I have a cool down to where the player can't just spam click and do massive damage in 1 second, but currently the player can just hold down the mouse to where it avoids that cooldown. I was hoping to find a way to force the box to turn back off after the user clicks down on the mouse. (Basically skipping the 'Input.Get$$anonymous$$eyUp' because the 'Get$$anonymous$$eyUp' isn't cutting it at the very moment.)

I am not really sure how to go about this because I want it to turn on for (maybe) .25 of a second then immediately deactivate the $$anonymous$$eleeBox again to where the player cannot just hold down to the mouse key.

Any ideas? :/

avatar image rmassanet Pur3 · Jul 20, 2016 at 07:09 AM 0
Share

Do you have an animation associated with that movement? If you do, you can enable and disable game objects from the animation itself.

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

7 People are following this question.

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

Related Questions

Trying to get Player Tip for player to open on Return and close on Return 2 Answers

Scale collider from one side for 2d Object 0 Answers

Double jump 2D Platformer 1 Answer

Trying to make the camera follow the player but stop at the edge. 1 Answer

2d: Problem with player running/walking between 2 box colliders/platforms 2 Answers


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