- Home /
Rigidbody Sticks to Walls
Hi, I've searched around for a answer but can't find one. Anyway, my rigidbody works fine except when it touches a wall when I jump and I hold forward it sticks the the wall. Anyone able to fix this?
using UnityEngine;
using System.Collections;
public class RigidController : MonoBehaviour {
public float walkSpeed = 10;
public float runSpeed = 20;
public float jumpForce = 200;
public float SpeedLimit = 50;
public bool isGrounded = false;
public bool inFront = false;
public LayerMask rayMask;
public float groundedCheckDist = 1;
public float moveCheckDist = 1;
private float Speed = 10;
void FixedUpdate () {
inFront = Physics.Raycast (transform.position, transform.forward, moveCheckDist, rayMask);
isGrounded = Physics.Raycast (transform.position, -transform.up, groundedCheckDist, rayMask);
Speed = Input.GetButton("Run") ? runSpeed : walkSpeed;
transform.rotation = Quaternion.Euler(Vector3.up * Camera.main.transform.localEulerAngles.y);
rigidbody.velocity = transform.TransformDirection(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized) * Speed + Vector3.up * ((Input.GetButtonDown ("Jump") && isGrounded) ? jumpForce : rigidbody.velocity.y);
rigidbody.velocity = Mathf.Clamp(rigidbody.velocity.magnitude, 0 , SpeedLimit) * rigidbody.velocity.normalized;
}
}
Have you tried setting the physic material and the colliders of both your character and the walls to something other than the default? You can import some sample physic materials:
Assets > Import Package > Physic $$anonymous$$aterials
Answer by vinfang · Nov 29, 2013 at 05:12 AM
If you look at the potato project that Unity provided for 4.3, you need to create a Physics2D material or Physics3D material, depending on your project. To create one, just right click in Project window of Unity and select Create -> Physics2D Material and then you can modify the material's value such as friction. The value 0 works well but 1 seems to work as well, just toy with the value to see what works well for your char.
Then on the collider objects like your wall, make sure the material is using the new physics material you created, and your char won't get stuck on the walls anymore.
3D game, checked the project and it doesn't exactly apply the same to what I'm doing. Plus, I don't want to addforce, I want a constant speed that comes to a stop. Not a gradual speed increase
Your method of changing the rigidbody's velocity shouldn't have to change. I'm surprised setting the physics material's friction to 0 didn't help your issue in terms of the character getting stuck to the wall though.
I was having the same problem , thanks so much for fixing this for me your a hero!
Yup, using physic material with a friction of 0 fixed it. But I'm now wondering if applying tons of physic material to level geometry will affect performance.
Setting friction to 0 on both solved this. Low values kind of worked too but ended up using 0 on both to avoid some small issues like being able to jump a bit higher whilst against a wall. Cheers.
Answer by Punfish · Mar 23, 2020 at 03:22 PM
The best solution I've been able to come up with is add multiple colliders on your unit, one which act as the feet and one slightly above the feet and slightly wider. The feet collider will have normal friction while the wider one would have no friction. This ensures you won't stick to walls as the slippery material will always touch the walls first, but also provide you with the proper ground friction to need to not slide everywhere.
Answer by mj2carlsbad · Mar 21, 2020 at 08:42 PM
On top of the other answer, also make sure your "Friction Combine" isn't set to Maximum or Multiply on either the player's physics material or the wall's physics material
This helped me figure out for the material on my character controller the friction combine needs to be set to minimum for the normal collider (not feet) so it is truly frictionless, average still adds on some friction from other objects so the collider would stick.
Your answer
Follow this Question
Related Questions
"Swimming" Physics with Rigidbodies 2 Answers
Why won't my rigidbody fps controller move? 1 Answer
Character Controller, changing collider's size? 0 Answers
Rigidbody move Character Controller 1 Answer