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

[SOLVED]Jumping with Character Controller is not smooth

Discussion in 'Scripting' started by KiarashRzg, Mar 18, 2020.

  1. KiarashRzg

    KiarashRzg

    Joined:
    Mar 16, 2020
    Posts:
    2
    Hi. I'm trying to learn unity and I've created a CharacterController script move, jump and crouch.
    everything works very fine except that jumping is not smooth at all !
    when i first press jump it will move instantly upwards and then falls smoothly; and its very annoying tbh.
    I really do need help on this because I've searched everywhere and didn't find any solutions.

    My script :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovementScript : MonoBehaviour
    4. {
    5.     public CharacterController controller;
    6.     public Transform player;
    7.  
    8.  
    9.     public float gravityMultiplier = 1f;
    10.     public float speed = 8f;
    11.     public float jumpHeight = 2.4f;
    12.  
    13.     Vector3 movement;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.         float x = Input.GetAxis("Horizontal");
    26.         float z = Input.GetAxis("Vertical");
    27.  
    28.  
    29.         // Crouch [ i dont have a character and its just a capsule and offline , so i just shrink it for crouch xD ]
    30.         if(Input.GetButtonDown("Crouch")) // Added a new axes called crouch with left shift
    31.         {
    32.             speed /= 3;
    33.             jumpHeight /= 1.5f;
    34.             player.transform.localScale *= 0.5f;
    35.  
    36.         }
    37.         if(Input.GetButtonUp("Crouch"))
    38.         {
    39.             speed *= 3;
    40.             jumpHeight *= 1.5f;
    41.             player.localScale = player.transform.localScale / 0.5f;
    42.  
    43.         }
    44.  
    45.  
    46.         movement = transform.right * x + transform.forward * z;
    47.         movement *= speed;
    48.  
    49.         // Applying Jump
    50.         if (Input.GetButtonDown("Jump")  && controller.isGrounded)
    51.         {
    52.  
    53.             movement.y = jumpHeight;
    54.  
    55.         }
    56.  
    57.         // Applying Gravity
    58.         if (controller.isGrounded == false)
    59.         {
    60.  
    61.             movement.y += Physics.gravity.y * gravityMultiplier;
    62.  
    63.         }
    64.  
    65.         // Applying Movement
    66.         controller.Move(movement * Time.deltaTime);
    67.  
    68.     }
    69. }
    70.  
     
  2. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    movement.y = jumpHeight;
    Sets the amount to jump very high initially.

    You're asking Unity to set the jump height to an arbitrary value and then hoping update and Time.deltaTime will smooth it up.

    You've not provided anything that looks like a speed modifier in your // Applying Movement section so unity is clocking off frames till it reaches target and you see a "jitter jump" to that location.

    You either need to jump to jumpHeight with a slerp function or scale the Time.deltaTime by some expected speed.
     
  3. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    Please check this documentation on how to perform the jump using a characterController:

    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    I think the problem is that you're resetting the movement vector every frame, so your jump code will set the jump height for 1 frame and then be overwritten.

    Notice in that documenation how it will only set the horizontal and vertical movement when the character is grounded.

    The only thing that happens to the movement vector while not grounded is gravity calculations.

    If you want to enable horizontal movement while mid-air, you need to get the current movement vector and add the horizontal movement vector to it.
     
  4. KiarashRzg

    KiarashRzg

    Joined:
    Mar 16, 2020
    Posts:
    2
    It was the case and adding everything except the gravity into an if(controller.isGrounded) solved everything
     
    Serinx likes this.
  5. A_j_ey

    A_j_ey

    Joined:
    May 26, 2020
    Posts:
    1
    Bro can u please explain me how u fixed your problem and what was actually causing it? I am trying to understand why it's happening.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I can't speak to the above question but it would probably be best for you to start a fresh thread yourself, as that is how the forum rules specify, and explain your issue clearly.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    For another example of a CharacterController script, here's some goodies:

    CharacterController CharMover broken:

    I wrote about this before: the Unity example code in the API no longer jumps reliably. I have reported it. Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746
     
  7. tomoe760

    tomoe760

    Joined:
    Apr 17, 2019
    Posts:
    1
    This code worked for me give it a try
    public CharacterController characterController;
    private Vector3 velocity;
    public float Speed = 2f;
    public float jump = 10f;
    public float Gravity = -9.8f;
    void update()
    {
    // for movement
    float horizontal = Input.GetAxis("Horizontal") * Speed;
    float vertical = Input.GetAxis("Vertical") *Speed;
    Vector3 move = transform.right * horizontal + transform.forward * vertical;
    characterController.Move(move * Speed * Time.deltaTime);
    // for jump
    if (Input.GetButtonDown("Jump") && transform.position.y< -0.51f)
    // (-0.5) change this value according to your character y position + 1
    {
    velocity.y = jump;
    }
    else
    {
    velocity.y += Gravity * Time.deltaTime;
    }
    characterController.Move(velocity * Time.deltaTime);
    }
     
    Last edited: Aug 9, 2021
    Ry167 and faisalurrehman234 like this.
  8. faisalurrehman234

    faisalurrehman234

    Joined:
    Aug 21, 2021
    Posts:
    2
     
  9. juliolima1225

    juliolima1225

    Joined:
    Dec 21, 2020
    Posts:
    1