- Home /
How can I guarantee a CharacterController never leaves the ground?
I have a CharacterController and I am trying to make absolutely certain that it never leaves the ground. More specifically, that it never leaves the y=0 plane.
The collision in the scenes are mostly at 90-degree angles (flat ground at y=0 and upright walls), but occasionally there are some places where collision is sloped (for example, a wooden pallet leaning against a wall). These areas of sloped collision are the problem. I want it such that if you try to move the CharacterController into that sloped collision, it doesn't climb or step on it in any way. But regardless of what I've tried it still climbs up this slope.
And actually the climbing behavior is a little odd. If I just try to move gently into the slope, it stays on the ground and doesn't climb. I can try moving more forcefully into the slope (i.e. - call CharacterController.Move() with a greater magnitude Vector3) and eventually the CharacterController will pop up and be standing about halfway up the slope.
I've set StepOffset to 0 and SlopeLimit to 0. I also make sure that every time, before I call CharacterController.Move(), the Vector3 I send in has a y-component of 0.
Is there any way to keep this from happening?
Answer by whydoidoit · May 26, 2012 at 03:57 PM
Firstly I'm no expert on CharacterController but this might help you... You could try attaching a script that forces the character back to the previous seen location with Y coordinate 0 - i.e. step back to the last valid position if you are in an invalid position:
var lastValidPosition : Vector3;
void Start()
{
lastValidPosition = transform.position; //Hopefully the starting point is valid!
}
void LateUpdate() {
if(transform.position.y != 0)
transform.position = lastValidPosition;
else
lastValidPosition = transform.position;
}
Yeah, this is basically the workaround that I had been using in the meantime. I hate this kinda stuff though because it feels so hacky to me.
Ultimately, I suspect that it is some odd bug with Unity physics and collision, but this workaround seems to be fine for now.
Well you could certainly say that it's a rule your system clearly defines, hence not a hack - but I know what you mean, you would like that no climbing thing to work with the parameters you supply.
Answer by Owen-Reynolds · May 27, 2012 at 04:08 PM
A non-programming approach would be to add some big box colliders to wall off the areas. A trick, if you have to place several adjoining, is to use a Unity cube, size it to the area, then click off the render. Can remove the mesh components when you are sure they are placed.
If you have other objects that need to pass through the area, you can set the colliders to only block the player: in Layer->AddLayer put something like playerWall in slot 8+. Then select playerWall for all those walls. Then add another layer "player" (maybe in slot 9) and put the player on that. Finally, in Edit->ProjectSettings->Physics, check off the playerWall boxes for everything except player (go down the playerWall column, and across the playerWall row, leaving only the box that crosses player checked.)
If you also have raycasts going into the area, those can be fixed to also ignore playerWall, but by then maybe a code-based solution is better.
Your answer
Follow this Question
Related Questions
3D Character controller jittering when jumping against a small wall 2 Answers
How to Logically Match Ground Slope While Using This Code? 1 Answer
Using rigidbody for collisions only, not movements? 1 Answer
How to hold objects in third person? 1 Answer
issue applying vector for slope sliding 0 Answers