- Home /
Character Controller, changing collider's size?
In our 2d platformer game, we have a trickster character that can change form and size. The Trickster character has a Character Controller (the First Person controller from the CharacterController package). When the trickster changes forms, we change the size of the character controller's center, radius, and height (it's always an upright capsule, I know we can't change that). The code we use is:
this.GetComponent<CharacterController>().center = formStats.colliderAlt;
this.GetComponent<CharacterController>().height = formStats.colliderHeight;
this.GetComponent<CharacterController>().radius = formStats.colliderRadius;
(formStats is just a script with a bunch of variables to change the size / form of the Trickster).
Here's the problem: if the collider changes from small (a spider) to large (a fox), there's a chance it can go through walls and floors. We know WHY this happens: when the collider suddenly gets bigger, it extends beyond the bounds of the wall ever so slightly. The Character Controller interprets this as going through the wall.
What's the best solution to this problem?
-Find some way to have walls "push" you back when you go from small to large? (no idea how to do this codewise) -Have separate layers for the Large form and Small form? (separate walls depending on collider size) -Just have the collider remain the same size? (which defeats the purpose of having a smaller form) -Some other solution?
Its tough to implement a general solution for this (which is one reason why there is none built in). One thing you can try is to cast rays (or spheres, etc.) in a circle from where the character is standing to try and see if it needs to push a bit before changing the collider size. In complex situations this may fail... such as if it is in a narrow corridor... you gotta ask yourself... what do you want to happen? Because in that case there is no "obvious" answer.
@nsxdavid Thanks for the suggestion with the sphere, we'll keep that in $$anonymous$$d. Narrow corridors aren't too much of a problem as we already have a script that prevents the player from changing to a larger form (we lay down an invisible box around the corridor, if they're within that box they can't change).