I am making a simple character controller. Whenever I continuously walk into a wall, gravity just stops working. For example, if there is a wall in front of me, and I jump and walk into it while in the air, I get stuck vertically. Here is a minimal example of my code:

[SerializeField] float speed; // player speed
private Rigidbody rb;

void Start() {
    rb = GetComponent<Rigidbody>(); // set rigidbody
void FixedUpdate() {
    float zInput = Input.GetAxisRaw("Vertical");
    float xInput = Input.GetAxisRaw("Horizontal");
    Vector3 targetVel = new(
        xInput == 0 ? 0 : xInput, 0, zInput == 0 ? 0 : zInput); // set new velocity
    targetVel = transform.TransformDirection(targetVel);

    targetVel *= speed; // go fast
    targetVel.y = rb.velocity.y; // keep existing y velocity

    rb.velocity = targetVel;

Also, I DO want the player to be able to move in midair.

  • \$\begingroup\$ How have you configured friction on the physics materials for the player object and wall objects, respectively? Usually zeroing out friction on vertical surfaces is enough to allow free sliding. \$\endgroup\$
    – DMGregory
    Aug 6, 2023 at 7:07
  • 1
    \$\begingroup\$ The conditions in targetVel can be removed, xInput and zInput placed direcly. Your condition basically says, if it is 0, place a zero there else just place the value. But if the value is zero already, it makes no difference if you use 0 or the value from your input (which is 0) \$\endgroup\$
    – Zibelas
    Aug 6, 2023 at 8:18
  • \$\begingroup\$ Thanks, adding a zero friction physic material to the player and wall worked! \$\endgroup\$ Aug 6, 2023 at 19:38
  • \$\begingroup\$ @DMGregory I think that would make an upvote-worthy answer. [Sorry, couldn't resist... Joking aside, would you rather a nudge or that someone expand into an answer on your behalf?] \$\endgroup\$
    – Basic
    Aug 7, 2023 at 23:01
  • 1
    \$\begingroup\$ In situations like this, I usually invite the user to answer their own question, because they have a working project handy and can screenshot the settings that worked for them, whereas I'd have to set up a test project first to verify any advice I'm giving. But I'd say it's first come first served, in terms of whoever wants to answer the question. I did a quick search for possible duplicates since I'm sure we've given the zero friction advice before, but I didn't find an obvious match in the 3D case. \$\endgroup\$
    – DMGregory
    Aug 7, 2023 at 23:09


You must log in to answer this question.

Browse other questions tagged .