- Home /
Drop through platform script
I am trying to make a script to drop through certain platforms. I have got it to almost work using collider2d.enabled = false; The problem is it only turns off one of the colliders on my character and I need it to turn off 2 colliders. Is there a way to specify which collider it should turn off? Here is the code for it so far
//platform fallthrough script
if (allowPlatformDropThrough) {
foreach (Transform groundCheck in groundChecks) {
RaycastHit2D fallThrough = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("fallThrough"));
if (grounded && fallThrough && Input.GetAxis ("Vertical") < 0) {
//rigidbody2D.collisionDetectionMode = false;
collider2D.enabled = false;
}
if (!fallThrough) {
collider2D.enabled = true;
}
}
}
About using more than one collider see here: http://answers.unity3d.com/questions/188775/having-more-than-one-collider-in-a-gameobject.html
Why do you need more than one collider?
the character has a box collider for their body and a circle collider for their feet. This is all done in 2d so the characters feet don't get caught on tiny edges.
Answer by tarasfromlviv · Jul 08, 2014 at 10:20 AM
You can just disable all the colliders at once.
var colliders = GetComponents<Collider2D>();
foreach (var col in colliders)
{
collider.enabled = false;
}
You can also check whether the collider isTrigger
inside loop to avoid turning off triggers or check col is BoxCollider2D
for instance to check if it is a collider of specific type.
I'm very new to scripting so all of that code was just a guess. I tried to add the bit you suggested but it still only seems to turn off the first collider in the inspector list. I added public Collider2D[] col; to the variables and attached each collider2d and modified the code to the following
//platform fallthrough script
if (allowPlatformDropThrough) {
foreach (Transform groundCheck in groundChecks) {
RaycastHit2D fallThrough = Physics2D.Linecast (transform.position, groundCheck.position, 1 << Layer$$anonymous$$ask.NameToLayer ("fallThrough"));
if (grounded && fallThrough && Input.GetAxis ("Vertical") < 0) {
var colliders = GetComponents<Collider2D>();
foreach (var col in colliders)
{
collider2D.enabled = false;
}
}
if (!fallThrough) {
collider2D.enabled = true;
}
}
}
I could add the full character controller code if you think it would help. It's a mess though.
Does anyone have an idea of how to disable all the colliders on a gameObject?