- Home /
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?
$$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.
definitely the thing about the start function. Also try using Get$$anonymous$$EyDown ins$$anonymous$$d of getkey
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.
Is the collider directly on your player, or is it a child of the player.
it is part of the player
Ill try the things you guys suggested above
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;
}
}
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?
omg THAN$$anonymous$$ YOU! I spent weeks trying to figure this out!
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;
}
}
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
Your suggestion is more appropriate as a comment, not an answer.
Your answer
Follow this Question
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