- Home /
Prevent character controller from changing movement direction when colliding with something.
Somewhat of an embarrassing question, but this is something that has been bugging me for a while. Currently, the character controller seems to behave in the following manner when colliding with something (top-down view):
Red: Hypothetical wall
Blue: Character Controller
Yellow: Input Velocity
Green: Final Velocity
How can i make it so that the controller stops moving in this scenario instead of changing velocity? CharacterController.Move()
is being used and i would like to avoid anything that would potentially involve SimpleMove
.
option one: in your assets folder right click and create new Physics material, set its friction to something high. set the material to additive friction. then select the wall and drop this material on the its collider's physics material slot.
option two: put your walls in the layer called 'walls'. then in your player script add:
void OnCollisionStay(Collider col){
if(col.gameObject.layer == "walls" && Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){
rigidbody.velocity = Vector3.zero;
}
}
Physic material based solution is kind of too much work, unfortunately. There has to be a better workaround. Option two just doesn't work. First of all there's no reason not to use tags ins$$anonymous$$d of layers. $$anonymous$$y character is controlled via a character controller and not a rigidbody. You'd set velocity to vector3 zero when the character collides with the wall and... doesn't want to move?
Looks like you are going to have to open up the character controller script and rewrite the collision detection script.
The default character controller is never going to be exactly what you need, and it's so bloated that it's really hard to rewrite. I'd suggest writing your own.
Your answer
Follow this Question
Related Questions
I want collision boxes to be precise without overlapping each other 1 Answer
Rigidbody2D.velocity out of controll 3 Answers
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer
How to get collision force, not relative velocity? 1 Answer
Rigidbody Wall Collision Bug/Wall Jitter 0 Answers