- Home /
How to slow character controller down when colliding with a wall?
I'm looking to replicate newton's second law with the character controller. As of now, the character controller stores a variable called "positionChange," which is a vector3, and every frame the character controller moves by it. This value does not decrease when colliding with walls though, so if the player holds down the movement keys against the wall, then their speed can infinitely increase in the direction of the wall, and additionally, the speed does not decrease, so when moving past the wall, the character controller accelerates to the full speed instantaneously.
How can I make it so that when colliding with walls, the character controller is pushed back in the opposite direction of the wall they are colliding with? Depicted in the above image is the character controller travelling in the direction of the x axis (red arrow). The character controller's "positionChange" vector has a value of 12 on the x axis, and if I were to remove the wall, the character controller would instantly begin moving 12 units per second instead of having to accelerate to 12 like in real life. If the character controller were to be pushed back equally in the direction they are colliding with the wall, then the speed against the wall would be 0, and the player would have to accelerate.
I want to make it so that the player's velocity/speed is pushed back against when they collide with objects, as depicted by the black arrow in the following image:
Please clarify your question, maybe with a drawing. What do you want to happen, and what is happening?
Answer by xthunderduckx · Oct 26, 2020 at 04:05 PM
Replicating the law of equal opposite reactions is actually a single line of code. First you need to understand which direction the force is being applied, which is given by the normal of a collision. Secondly, applying the dot product between the current velocity and player velocity produces the amount of force. Multiply this by the normal and you get the amount of force (in the form of a Vector3) to be applied back to the player's current velocity.
It looks like this: playerVelocity -= collision.normal * Vector3.Dot(playerVelocity, collision.normal);
, and it runs in onControllerColliderHit().
Answer by Sspyrshlsx · Jul 10, 2020 at 09:05 AM
For pushing back, have you tried using a bouncy physics material? I don't know if it will work but better give it a try.
Also a possible fix for accelerating on walls is to check if the player's rigidbody has a velocity of 0 on a certain axis. You can check my comment on this post to see what I mean, which hopefully helps.
I know this is like a year late by this point but I did figure it out. This is a character controller so it does not use physics materials.
Your answer
![](https://koobas.hobune.stream/wayback/20220612214742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Determinism of collision judgment of Character Controller 0 Answers
How to setup character Collisions? 2 Answers
How to prevent Character Controller from falling through moving Platforms? 4 Answers
CharacterController jumps when colliding with kinematic rigidbodies 1 Answer
Limiting rigidbody physics influence on another rigidbody 0 Answers