Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why is my FPS character sticking to walls and then falling down?

Discussion in 'Scripting' started by maxveris, Sep 6, 2020.

  1. maxveris

    maxveris

    Joined:
    Oct 25, 2018
    Posts:
    3
    Hi. I got this really simple FPS game, but when you jump against a wall, the player sticks for a while and then falls down (see video linked below). How can I solve this problem? It's not a problem with friction (I've already tested this by adding a physics material that has no friction). I also added my player controller script. I would be really gratefull if someone could help me out!

    Video of what happens: https://imgur.com/a/78TdEWU

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent(typeof(Rigidbody))]
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float jumpForce = 3f;
    10.     public float walkSpeed = 3f;
    11.  
    12.     // Input
    13.     float horizontal;
    14.     float vertical;
    15.  
    16.     Vector3 deltaPosition;
    17.  
    18.     bool isGrounded;
    19.     bool isJumping;
    20.  
    21.     Rigidbody rb;
    22.  
    23.     void Start()
    24.     {
    25.         rb = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         horizontal = Input.GetAxis("Horizontal");
    31.         vertical = Input.GetAxis("Vertical");
    32.  
    33.         Jump();
    34.     }
    35.  
    36.     private void FixedUpdate()
    37.     {
    38.         deltaPosition = ((transform.forward * vertical) + (transform.right * horizontal)) * walkSpeed * Time.fixedDeltaTime;
    39.         rb.MovePosition(rb.position + deltaPosition);
    40.     }
    41.  
    42.     void OnCollisionEnter(Collision other)
    43.     {
    44.         if (other.gameObject.tag == "Ground")
    45.         {
    46.             isGrounded = true;
    47.         }
    48.     }
    49.  
    50.     void Jump()
    51.     {
    52.         if (Input.GetButtonDown("Jump") && isGrounded == true)
    53.         {
    54.             isGrounded = false;
    55.             rb.velocity += (Vector3.up * jumpForce);
    56.             isJumping = true;
    57.         }
    58.         else if (Input.GetButtonDown("Jump") && isJumping == true)
    59.         {
    60.             rb.velocity += (Vector3.up * jumpForce);
    61.             isJumping = false;
    62.         }
    63.     }
    64. }
    65.  
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Is your wall tagged as Ground ?
     
    Lethn likes this.
  3. maxveris

    maxveris

    Joined:
    Oct 25, 2018
    Posts:
    3
    No, it isn't set as ground. Setting it as ground doesn't change anything however.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You hit the wall pretty close to the highest point in the jump, where the velocity.y should be nearly 0. How much longer do you stay in the air when bumping into a wall, than if you just jumped normally (like in the video, just without a wall there)?
     
  5. maxveris

    maxveris

    Joined:
    Oct 25, 2018
    Posts:
    3
    I made another video where I show this: https://imgur.com/sZG6fQP
     
  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    I see your character controller is dynamic not kinematic since you are setting velocity when jumping.
    Yet you are using Rigidbody.MovePosition during the FixedUpdate.
    This might be the case. If you want proper collision done with physics you have to use Rigidbody.AddForce.
    Or go with the kinematic rigidbody and resolve collisions by yourself.