Search Unity

Inconsistent jumping..?

Discussion in 'Scripting' started by Vexer, Mar 16, 2019.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey Unity,
    I decided to start working on an old project of mine again and I've ran into an issue I've ran into before and I did manage to fix it after some help on here so I was wondering if you guys could help me fix this again :)

    So the issue that I'm having right now is that if I stand still and jump I just the way I want my character to jump BUT if I run and jump at the same time my character for some reason jumps higher than it should, this is my code if has a solution please let me know!:

    https://gyazo.com/164b143f3ccc58808aaba58e1b1f6ed9

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     public AudioClip jump;
    9.  
    10.     private AudioSource audioSource;
    11.  
    12.     public float Speed = 5;
    13.  
    14.     private Rigidbody2D rb;
    15.  
    16.     public bool isGrounded;
    17.  
    18.     public LayerMask whatIsGround;
    19.  
    20.     private Collider2D col;
    21.  
    22.     public bool JumpSoundPlaying = false;
    23.  
    24.     public float JumpVelocity = 10;
    25.  
    26.     // Use this for initialization
    27.     void Start()
    28.     {
    29.  
    30.         rb = gameObject.GetComponent<Rigidbody2D>();
    31.  
    32.         audioSource = GetComponent<AudioSource>();
    33.  
    34.         col = GetComponent<Collider2D>();
    35.     }
    36.  
    37.     void FixedUpdate()
    38.     {
    39.         isGrounded = Physics2D.IsTouchingLayers(col, whatIsGround);
    40.         if (Input.GetKeyDown("space"))
    41.         {
    42.             if (isGrounded)
    43.             {
    44.                 rb.AddForce(new Vector2(0, JumpVelocity), ForceMode2D.Impulse);
    45.                 if (!JumpSoundPlaying)
    46.                 {
    47.                     StartCoroutine("JumpSound");
    48.                 }
    49.             }
    50.         }
    51.  
    52.         float moveHorizontal = Input.GetAxisRaw("Horizontal");
    53.  
    54.         Vector2 movement = new Vector2(moveHorizontal, 0);
    55.  
    56.         transform.Translate(movement * Speed);
    57.  
    58.         /* -=== Auto run ===-
    59.         transform.Translate(Speed * Time.deltaTime, 0f, 0f);
    60.            -=== Auto run ===- */
    61.     }
    62.  
    63.     IEnumerator JumpSound()
    64.     {
    65.         audioSource.clip = jump;
    66.         audioSource.Play();
    67.         JumpSoundPlaying = true;
    68.         yield return new WaitForSeconds(0.1f);
    69.         JumpSoundPlaying = false;
    70.     }
    71. }
    72.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You might want to consider splitting up your
    FixedUpdate
    . That method can be called more (or less) than once per frame. If it gets called more than once, then line 56 will move the object more than expected.

    You only really want line 39 in
    FixedUpdate
    - move the rest into
    Update
    . See if that fixes the problem.
     
    Flaring-Afro likes this.
  3. Flaring-Afro

    Flaring-Afro

    Joined:
    Jul 20, 2015
    Posts:
    14
    To add to Doug's comment, it can happen less than once per frame if the frame rate is higher than 50 (or more, if you changed the rate in project settings). This means if a frame that received the new key down when polling input does not have fixed update run, the input will be loss. The player could press the space bar, even hold it down, and nothing happen.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    And not only that, the only input getting you wanna do in fixed update is GetKey, the normal one not GetKeyDown or GetKeyUp, they refresh by the visual frame's update and you will miss your inputs if they don't run side by side (or frame-by-frame).

    Take the input in Update() and flag a logic gate in FixedUpdate to jump when the next physics tick happens.