- Home /
Disabling colliders not actually working
So I'm working on a beat-em-up type of action game where players can pick up boxes and throw them. However, I noticed that when a box was lifted over a character's head, the box collider on the objects would actually stop a player from jumping (they would "hit their head" on the box and come right back down). So of course, I decided to disable all colliders on the object so that this wouldn't happen, and instead extend the player's own collider up to compensate.
Here was the relevant code:
class BaseItem extends MonoBehaviour
{
function PickedUp(parentTransform : Transform){
//For the box collider
this.collider.enabled = false;
//This item also has a trigger collider
var triggerCollider : CapsuleCollider = this.GetComponent(CapsuleCollider);
triggerCollider.enabled = false;
}
}
However, this was when I was experimenting with all my code inside of the BaseItem class.
When I decided to make a separate ThrowableItem class, I moved this code into the overridden version of the function:
class ThrowableItem extends BaseItem
{
override function PickedUp(parentTransform : Transform){
//For the box collider
this.collider.enabled = false;
//This item also has a trigger collider
var triggerCollider : CapsuleCollider = this.GetComponent(CapsuleCollider);
triggerCollider.enabled = false;
}
}
Now, for whatever reason, the colliders aren't disabling... Even though the Unity editor says that they are disabled. That's right... The Unity editor claims that the colliders have been turned off, even though the game still registers them being there.
I can even delete the colliders entirely off of the item mid-game and they will still register as being there. How is that possible? If they're deleted, why are they still there?
I'm supposing this might have something to do with ThrowableItem being a child of BaseItem, but I can't figure out why it happens this way and how to change it.
Any help is greatly appreciated!
Wait, what's this? I just realized that the object has a new SphereCollider inside of the box that came out of nowhere!
And even stranger, it's not listed under the object's components, either!
Have I come across a bug in Unity...?
I can't access the mysterious new collider through scripting, either... What on earth happened, I wonder?
Answer by usagijojo · Aug 18, 2013 at 10:40 AM
Ahhh, I figured out my problem!
I accidentally added this to my BaseItem code:
@script RequireComponent(CharacterController)
Copied over from the BasePlayer class. Whoops! Sorry for troubling you guys.
Glad you found it. I've converted your comment to an answer.
Answer by whydoidoit · Aug 18, 2013 at 09:21 AM
If you are going to move or change colliders then the associated object needs to have a rigidbody to keep the physics system up to date. This rigidbody can be set to isKinematic = true to avoid it actually doing physics calculations.
Basically colliders are not expected to move or disappear if they aren't attached to some kind of rigidbody.
Though that really doesn't explain why it ever worked of course!
I added Rigidbody, and set is$$anonymous$$inematic = true... But it didn't change anything.
$$anonymous$$eep in $$anonymous$$d that even deleting the colliders manually doesn't seem to do anything, either.
Yep, but just for reference it's necessary to do that to avoid weirdness and performance penalties. Obviously something else as we'll in this case.