- Home /
How prevent my player from getting stuck on the wall/clinging onto it if I am moving towards it (rb.velocity) with an arrow key at the same time?
I am making a 3D game where the character moves around in a world. When I touch a wall (even in midair) and I press an arrow key which is moving the player in the direction towards the wall, he gets stuck. How do I make it so that he just slides off like normal, such as Mario when he just falls/slides off of the wall?
My Code:
public class Player : MonoBehaviour { public Rigidbody rb; private float Movespeed = 10f; public GameObject player; public Animator player_anim; public bool grounded; public float jumpForce;
 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent<Rigidbody>();
     grounded = false;
 }
 private void FixedUpdate()
 {
     rb.velocity = new Vector3(Input.GetAxis("Horizontal") * Movespeed, rb.velocity.y, Input.GetAxis("Vertical") * Movespeed);
     if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
     {
         player_anim.SetBool("Moving", true);
     }
     if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
     {
         player_anim.SetBool("Moving", false);
     }
     Vector3 rotation = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     if (rotation != Vector3.zero)
     {
         player.transform.rotation = Quaternion.LookRotation(rotation);
     }
     
     if (Input.GetKeyDown(KeyCode.Space))
     {
         Jump();
     }
     
 
 }
 public void Jump()
 {
     if (grounded)
     {
         
         rb.AddForce(player.transform.up * jumpForce);
         player_anim.SetBool("Jump", true);
         grounded = false;
     }
     
 }
  void OnCollisionEnter(Collision other)
 {
    if (other.gameObject.tag == "ground" || other.gameObject.tag == "Enemy" || other.contacts[0].normal.y < 1.3f && other.contacts[0].normal.y > 0.7f) //last one is any other flat surface
    {
         if(other.contacts[0].normal.y < 1.3f && other.contacts[0].normal.y > 0.7f)
         {
             grounded = true;
             Debug.Log(other.contacts[0].normal);
             player_anim.SetBool("Jump", false);
         }
         
        
    }
 }
}
create a physics material, set a low dynamic friction, a bouciness up than 0.1f and set friction combine to $$anonymous$$imium, add it to the player colliders and check the result fit your desire, otherwise play a little with the variables to make it more "slippery"
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                