- Home /
Rigidbody Player gets stuck on walls when jumping
So, I am working on a fps controller that uses a rigidbody instead of Unity's Character Controller. I am currently running into a problem where when the player jumps then touches a wall, they will stop moving upward for a second before falling.
I have discovered that the player is still doing their full jump, but won't go any higher when touching a wall. I also discovered that using Rigidbody.AddForce causes this problem only. Rigidbody.MovePosition won't. I'm trying to figure out how to do this without using a Physics material on every collidable object in the game
Here is my movement code
private void Update()
{
float v = Input.GetAxis("Vertical") * MoveSpeed;
float h = Input.GetAxis("Horizontal") * StrafeSpeed;
velocity = new Vector3(h, velocity.y, v);
velocity = transform.TransformDirection(velocity);
if (IsGrounded())
{
if (Input.GetKeyDown(KeyCode.Space))
{
RB.AddForce(transform.up * Mathf.Sqrt(JumpModifier * JumpForce * Gravity));
}
}
}
private void FixedUpdate()
{
RB.MovePosition(RB.position + velocity * Time.deltaTime);
}
I have the same issue also with MovePosition. Have you managed to somehow resolve the issue yet?
My solution, which I found out 3 years later, was an easy-ish solution. Instead of using MovePosition, I ended up using AddForce. Now, unlike MovePosition, AddForce adds a force to the player. I'm adding my code as an answer so you can see what I'm doing
Answer by ItsKhanny · Feb 26 at 08:36 AM
So, I'm now doing two things, using AddForce and using a PhysicsMaterial. So, here is my code for adding force to the player's movement
var velocity = m_Rigidbody.velocity;
var velocityChange = moveDirection - velocity;
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
m_Rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
maxVelocityChange is just a float variable I have currently set to 20. Along with this, I do apply a PhysicsMaterial to the player with a friction of 0. This removes the issue of getting stuck on walls and the player can move fairly freely
Answer by RustyCrow · May 24, 2019 at 08:47 PM
Just assign the material on the player maybe? that way you avoid doing it on the all the other stuff
I've tried adding it to my player, but the it doesn't do anything different
Answer by mpodlesny · Jul 24, 2020 at 06:44 PM
Assign a physics 2d material to your character then set the friction to zero on the material
Your answer
![](https://koobas.hobune.stream/wayback/20220612202035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making RB movement smoother/curvy. C#. 1 Answer
How to push a player in a certain direction? 2 Answers
Ball moving much faster forward when there is horizontal input 0 Answers
Rigidbody jumpPad Not working 0 Answers
Multiple Cars not working 1 Answer