Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Character Jumping Strange While Moving Up Slopes

Discussion in 'Physics' started by Rauza3107, Feb 29, 2024.

  1. Rauza3107

    Rauza3107

    Joined:
    Jul 19, 2022
    Posts:
    4
    Hello,
    I have made an FPS player movement script using CharacterController and there is an issue when the player is moving up a slope and jumps. It seems that when the player jumps up a slope it does a 'mini jump' sorta thing, and if I stop during that jump the player continues the jump upwards?

    It's hard to explain so I made this really bad diagram:
    JumpDiagram.png

    Here is the code I use to make the player jump:

    if (Input.GetKeyDown(JumpKey) && isGrounded)
    {
    velocity.y = Mathf.Sqrt(JumpHeight * -2f * Gravity);
    }
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    This is a result of passing the input values directly into CharacterController.Move. When you release input the character quickly stops moving on the horizontal plane but it still has the upward velocity from the jump.

    It's possible to give the character controller more realistic movement or inertia by adding the input to a velocity vector but it'll still be a little quirky in certain situations because the character controller wasn't made to be realistic. Alternatively you could consider using a rigidbody based character.
     
  3. Rauza3107

    Rauza3107

    Joined:
    Jul 19, 2022
    Posts:
    4
    I'm not really going for realistic movement, that is just the only way I know how to make the player jump. Is there an alternative method that will stop this issue from occurring?
     
  4. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    38
    I would suggest using a rigidbody based character, they are usually much better. If you don't know how to make one of these, look up any number of tutorials on google, there's a billion of them.

    (Rigidbody is better because it allows for physics interactions that make more sense, and allows for many more options, especially later when you might be adding additional mechanics to your game)
     
  5. Rauza3107

    Rauza3107

    Joined:
    Jul 19, 2022
    Posts:
    4
    So there isn't a solution to the problem I'm having and switching to a Rigidbody based movement system would stop this from happening?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,221
    You just need to kill any upwards velocity if your controller passes a grounded check, though likely only after a 'grace period' after they have initially jumped.
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    A character controller with inertia:
    Code (CSharp):
    1. using UnityEngine;
    2. public class SimpleController : MonoBehaviour
    3. {
    4. CharacterController cc;
    5.  
    6.     void Start()
    7.     {
    8.         cc = GetComponent<CharacterController>();
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         Vector3 velocity=cc.velocity+(Physics.gravity*Time.deltaTime);  // Get the current "velocity" and add gravity
    14.         if (Physics.SphereCast(transform.position,0.5f,Vector3.down,out RaycastHit hit,0.6f))  // CharacterController's isGrounded can be unreliable when jumping up slopes and so I'll use SphereCast instead
    15.         {
    16.             if (Input.GetButtonDown("Jump"))
    17.                 velocity.y+=5;
    18.             else
    19.             {
    20.                 velocity+=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized*50*Time.deltaTime;
    21.                 velocity-=velocity*4*Time.deltaTime; // ground friction
    22.                 velocity.y=-6; // helps to keep the character pinned to the ground when going down slopes
    23.             }
    24.         }
    25.         cc.Move(velocity * Time.deltaTime);
    26.     }
    27. }
     
  8. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    38
    There is a solution, but more problems like this will probably appear as you continue development, which is why I suggest switching to a rigidbody base.

    While you will still need to fix this problem with a rigidbody base, it will be easier than continuing with the character controller.
     
    Last edited: Mar 4, 2024