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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question [FIXED] Inconsistent Jump Height with CharacterController-based Jumping Script

Discussion in 'Scripting' started by Swimmtrunk, Jun 13, 2021.

  1. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    I've been working with this character controller that adjusts with Cinemachine camera movement and I wanted to create a jump script for the player to use, this is the following script I have:

    upload_2021-6-12_22-3-14.png

    But what I'm now having trouble with is that -
    1. I press my "Jump" key (in this case, space) and it doesn't jump at all
    2. I do jump, but this can range from a small hop, all the way to a huge leap in the air

    I had taken a break and come back after a few days and after looking over it with "fresher" eyes, my complications really just seem to come down to the keybind section (the main if statement). Is it possible this is an error in the frequency in which the Update() function is called? Any help is appreciated, as I've been stuck on this problem and other threads and videos I've looked at apply to a different kind of character controller and not the actual Character Controller component itself.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You can use [ code ] tags to share code here.

    As for your issue, you've been accumulating gravity into the velocity non-stop since you started the game. Your jump has basically no chance of overcoming that force unless you bounce like a rabbit the entire time you play. Instead of adding to the velocity with
    +=
    , explicitly set the velocity with
    =
    when you jump.
     
  3. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    Thank you for both the help and the tip about sharing code, the problem is fixed!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954