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.

Rootmotion controlled character jumping, character dashes too far when you move and jump.

Discussion in 'Getting Started' started by SamzUel, Nov 29, 2022.

  1. SamzUel

    SamzUel

    Joined:
    Sep 20, 2020
    Posts:
    4
    I have a rootmotion controlled character, and I've added jumping. But when you jump while moving the character dashes really far in those directions. And its worse when you jump while sprinting, the character shoots forward really far. My lecturer says to make the velocity the animations give to be limited/ clamped, but I have no idea how to do that.
     

    Attached Files:

    Last edited: Nov 29, 2022
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    534
    You take your existing velocity (which already has magnitude of character speed/second) and multiply it by character speed again. So the result is velocity squared.
     
    SamzUel likes this.
  3. SamzUel

    SamzUel

    Joined:
    Sep 20, 2020
    Posts:
    4
    Not sure where to add the "* characterSpeed" wherever I add it seems to make the character jump even higher and further than before.
    Code (CSharp):
    1. public class CharacterMovement : MonoBehaviour
    2. {
    3.     public InputManager iManager;
    4.  
    5.     [Header("Movement")]
    6.     [SerializeField] bool holdToRun;
    7.     [SerializeField] bool isCrouch;
    8.     [SerializeField] float characterSpeed;
    9.     Animator anim;
    10.     CharacterController cController;
    11.     Vector2 moveInput;
    12.     Vector3 rootmotion;
    13.  
    14.     [Header("Jump")]
    15.     [SerializeField] bool isJump;
    16.     [SerializeField] float stepDown;
    17.     [SerializeField] float jumpHeight;
    18.     [SerializeField] float gravity;
    19.     [SerializeField] float airControl;
    20.     [SerializeField] float jumpDampTime;
    21.     [SerializeField] Vector3 velocity;
    22.     void Start()
    23.     {
    24.         anim = GetComponent<Animator>();
    25.         cController = GetComponent<CharacterController>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         moveInput.x = Input.GetAxis("Horizontal");
    31.         moveInput.y = Input.GetAxis("Vertical");
    32.  
    33.         anim.SetFloat("InputX", moveInput.x);
    34.         anim.SetFloat("InputY", moveInput.y);
    35.  
    36.         Sprinting(iManager.running);
    37.         Crouch(iManager.crouch);
    38.     }
    39.  
    40.     void OnAnimatorMove()
    41.     {
    42.         rootmotion += anim.deltaPosition;
    43.     }
    44.  
    45.     void FixedUpdate()
    46.     {
    47.         cController.Move(rootmotion);
    48.         rootmotion = Vector3.zero;
    49.  
    50.         if (isJump)
    51.         {
    52.             UpdateInAir();
    53.         }
    54.         else
    55.         {
    56.             UpdateOnGround();
    57.         }
    58.     }
    59.  
    60.     public void Crouch(KeyCode button)
    61.     {
    62.         if (Input.GetKeyDown(button))
    63.             isCrouch = !isCrouch;
    64.  
    65.         if (Input.GetKeyDown(button))
    66.             anim.SetBool("Crouch", isCrouch);
    67.  
    68.     }
    69.  
    70.     public void Sprinting(KeyCode button)
    71.     {
    72.         if (Input.GetKeyDown(button))
    73.         {
    74.             holdToRun = true;
    75.             characterSpeed = 3.5f;
    76.             anim.SetBool("Running", true);
    77.         }
    78.         if (Input.GetKeyUp(button))
    79.         {
    80.             holdToRun = false;
    81.             characterSpeed = 1.9f;
    82.             anim.SetBool("Running", false);
    83.         }
    84.     }
    85.  
    86.     void UpdateOnGround()
    87.     {
    88.         Vector3 stepForwardAmount = rootmotion * characterSpeed;
    89.         Vector3 stepDownAmount = Vector3.down * stepDown;
    90.  
    91.         cController.Move(stepForwardAmount + stepDownAmount);
    92.         rootmotion = Vector3.zero;
    93.  
    94.         Jump(iManager.jump);
    95.     }
    96.  
    97.     void UpdateInAir()
    98.     {
    99.         velocity.y -= gravity * Time.fixedDeltaTime;
    100.  
    101.         Vector3 displacement = velocity * Time.fixedDeltaTime;
    102.         displacement += CalculateAirControl();
    103.  
    104.         cController.Move(displacement);
    105.         isJump = !cController.isGrounded;
    106.         rootmotion = Vector3.zero;
    107.         anim.SetBool("Jump", isJump);
    108.     }
    109.  
    110.     Vector3 CalculateAirControl()
    111.     {
    112.         return ((transform.forward * moveInput.y) + (transform.right * moveInput.x)) * (airControl / 100);
    113.     }
    114.  
    115.     public void Jump(KeyCode button)
    116.     {
    117.         if (Input.GetKeyDown(button))
    118.         {
    119.             if (!isJump)
    120.             {
    121.                 isJump = true;
    122.                 velocity = anim.velocity * jumpDampTime * characterSpeed;
    123.                 velocity.y = Mathf.Sqrt(2 * gravity * jumpDampTime);
    124.                 anim.SetBool("Jump", true);
    125.             }
    126.         }
    127.     }
    128. }
     
  4. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    534
    Sorry, didnt make it clear. You do not add "character speed" anywhere, you remove it. Velocity being squared was a mistake

    velocity = anim.velocity * jumpDampTime * characterSpeed;
    should be
    velocity = anim.velocity * jumpDampTime;

    Also, I believe in

    velocity.y = Mathf.Sqrt(2 * gravity * jumpDampTime);

    instead of jumpDampTime, you need to use a different "jump height" variable
     
    SamzUel likes this.
  5. SamzUel

    SamzUel

    Joined:
    Sep 20, 2020
    Posts:
    4
    IT WORKS! Thank you soo much!