Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 archerassassin8 · May 12, 2015 at 10:51 AM · scripting problemscript.2d-physics

How to disable a collision box with a keyboard key

I made a script to disable a collision box for my character. The script was supposed to disable a collision box for my characters head when I crouch (Left Control). I don't get any errors in unity. Here is my c# script:

 public GameObject playerObject;
     
     
     void Update()
     {
         if (Input.GetKey (KeyCode.LeftControl))
         {
             playerObject.GetComponent<BoxCollider2D>().enabled = false;
             
         }
         else
         {
             playerObject.GetComponent<BoxCollider2D>().enabled = true;
         }
     }

I tried to change the ".enabled" with ".isTrigger" but it still didn't work. I made sure to direct the "player.object" to the character in the inspector. My question is, am I missing something small but important? Or did I screw up with the script entirely?

Comment
Add comment · Show 6
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 KdRWaylander · May 12, 2015 at 11:04 AM 0
Share

$$anonymous$$mm seems good to me, what do you mean by 'it doesn't work' ? When you add a debug log in your 'if' or your 'else' what happens ? Does the game return these debugs ?

Little tip here: you shouldn't do a GetComponent in update, find the component in Start(), assign it to a variable and use this variable in Update(), it's less overhead.

avatar image melkorinos · May 12, 2015 at 02:49 PM 0
Share

definitely the thing about the start function. Also try using Get$$anonymous$$EyDown ins$$anonymous$$d of getkey

avatar image PatriceVignola · May 12, 2015 at 03:44 PM 2
Share

You should probably use $$anonymous$$eyDown to disable the collider and $$anonymous$$eyUp to enable it ins$$anonymous$$d of forcing it every frame, but otherwise the code seems to be fine.

  • Can youd debug the if/else statements with a log or breakpoints?

  • Is there another place where you enable/disable this collider in an update? $$anonymous$$aybe the problem lies there.

avatar image Dblfstr · May 12, 2015 at 06:32 PM 0
Share

Is the collider directly on your player, or is it a child of the player.

avatar image archerassassin8 · May 12, 2015 at 08:35 PM 0
Share

it is part of the player

Ill try the things you guys suggested above

Show more comments

2 Replies

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

Answer by KdRWaylander · May 18, 2015 at 06:36 AM

Hey, sorry for late answer:

What you do in your piece of code right now is, at every frame, the game inspects all the components of the object to find the one it has to interact with. The idea here is to tell what component the game has to use since the beginning so it has no need to scan for it at each frame.

What it would look like (answer if you need more explanations on it):

 BoxCollider2D boxColliderComponent;
 
 void Start () {
    boxColliderComponent = GameObject.Find("Name of the object").GetComponent<BoxCollider2D>();
 }
 
 void Update () {
    if(Input.GetKeyDown(KeyCode.LeftControl)){
       boxColliderComponent.enabled = false;
    } else if (Input.GetKeyUp(KeyCode.LeftControl)) {
       boxColliderComponent.enabled = true;
    }
 }
Comment
Add comment · Show 4 · 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 abhi_360 · May 18, 2015 at 06:41 AM 0
Share

yeah agreed try this

avatar image archerassassin8 · May 18, 2015 at 04:22 PM 0
Share

Would this work if I have multiple box colliders? I have one that I want to disable for crouching, one for the body, and then a circle collider for the legs. Someone told me that i wont be able to disable it if I have more than one of the same collider. Is this true?

avatar image archerassassin8 · May 18, 2015 at 11:54 PM 0
Share

omg THAN$$anonymous$$ YOU! I spent weeks trying to figure this out!

avatar image KdRWaylander · May 19, 2015 at 06:48 AM 0
Share

You're welcome ;)

You can only have up to one collider of the same type on each gameobject.

Here is a code sample that should show you the possibilities you have to disable multiple colliders with the same script as above (some collider types or objects can be equal):

 ColliderType1 ColliderComponent1;
 ColliderType2 ColliderComponent2;
 ColliderType3 ColliderComponent3;
 
 void Start () {
     ColliderComponent1= GameObject.Find("Name of the first object").GetComponent<ColliderType1>();
     ColliderComponent2= GameObject.Find("Name of the second object").GetComponent<ColliderType2>();
     ColliderComponent3= GameObject.Find("Name of the third object").GetComponent<ColliderType3>();
 }
 
 void Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftControl)){
         ColliderComponent1.enabled = false;
         ColliderComponent2.enabled = false;
         ColliderComponent3.enabled = false;
     } else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftControl)) {
         ColliderComponent1.enabled = true;
         ColliderComponent2.enabled = true;
         ColliderComponent3.enabled = true;
     }
 }

avatar image
0

Answer by Sethhalocat · May 12, 2015 at 07:11 PM

Have you tried a delete.this statement after your key down, if not then i would reccomend that you search up how to use it. Tell me if it worked :D

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 Dblfstr · May 12, 2015 at 07:44 PM 1
Share

Your suggestion is more appropriate as a comment, not an answer.

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

6 People are following this question.

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

Related Questions

How can I make that the interactable raycast shoot will not pass through some objects like doors ? 1 Answer

Why when i loop over the vertices it's not showing the mesh ? 1 Answer

How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers

How to change button text in simple character shop ? 1 Answer

How can i make the transform rotation Quaternion.identity but smooth ? 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