- Home /
How do I make my Rigidbody player not get stuck on walls?
My rigidbody player that I made keeps on getting stuck on walls if I jump and hold the direction key on the wall and I dont know how to fix it.
Here is the code I am using, mostly a mash up of code I used from tutorials because I am not an experienced programmer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
/////////////////////
////variable zone////
/////////////////////
public Rigidbody playerBody;
private Vector3 inputVector;
public float speed = 10f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private bool isGrounded;
public float jumpHeight = 3f;
/////////////////////
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
inputVector = new Vector3(Input.GetAxis("Horizontal") * speed * 20 * Time.deltaTime, playerBody.velocity.y, Input.GetAxis("Vertical") * speed * 20 * Time.deltaTime);
transform.LookAt(transform.position + new Vector3(inputVector.x, 0, inputVector.z));
playerBody.velocity = inputVector;
if(Input.GetButtonDown("BlobbyJump") | Input.GetButtonDown("AllJump") && isGrounded)
{
playerBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
}
}
}
Answer by franconecat · Jul 29, 2020 at 10:42 PM
Unity has a Physics engine that tries to imitate real-world physics. In the real world, there is something called friction. When two objects are tightly touching each other they won't move. Like a Hook-and-loop fastener.
To fix that in your game you can apply a Physics Material with 0 Friction to your player's rigid body. This will tell Unity's physics engine that your player won't have any friction.
In my game, this prevents the player from being attached to a wall.
Screenshots of my player's rigid body with a physics material:
Documentation for 3D: https://docs.unity3d.com/Manual/class-PhysicMaterial.html
Documentation for 2D: https://docs.unity3d.com/Manual/class-PhysicsMaterial2D.html
Answer by jmhoubre · Jul 29, 2020 at 08:54 PM
Hello,
you should use physics in FixedUpdate rather than Update. But test if player jumps in Update. With a boolean :
private bool is Jumped;
private void Update () {
isJumped = (Input.GetButtonDown("BlobbyJump") | Input.GetButtonDown("AllJump")) && isGrounded;
}
private void FixedUpdate() {
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isJumped)
{
isJumped = false;
playerBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
}
}
Why not beginning with a more simple script ?
Sorry for any mistakes in English.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using isGrounded with a RigidBody 2 Answers
Multiple Cars not working 1 Answer
Jumping with rigid body3d 1 Answer
Distribute terrain in zones 3 Answers
Why wont my character jump? (using rigidbody and addForce) 1 Answer