Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Jumping while moving modify the jump height

Discussion in 'Scripting' started by Stefloper111, Nov 6, 2018.

  1. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Hi guys, i was testing my game when i have noticed something potentially strange
    Its a 2D Platformer, so i can jump (More or less high depends on how long u push the key), and moving
    And of course moving in air etc
    But if i move, then i jump, my jump will be more high that if i was stationnary
    can i do something for resolve that ?
     
  2. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    24
    Please show us the script responsible for player's moving and jumping
     
  3. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    The jump script :

    Code (CSharp):
    1.     void Jump()
    2.     {
    3.         if (Input.GetButton("Jump"))
    4.         {
    5.             jumpTimeCounter -= Time.deltaTime;
    6.             if (jumpTimeCounter > 0f)
    7.             {
    8.                 jumpKeyHeld = true;
    9.             }
    10.         }
    11.         else if (Input.GetButtonUp("Jump"))
    12.         {
    13.             jumpKeyHeld = false;
    14.             jumpTimeCounter = jumpTime;
    15.         }
    16.  
    17.         if (jumpKeyHeld)
    18.         {
    19.             if (numberOfJump < 2)
    20.             {
    21.                 rb.velocity = new Vector2(rb.velocity.x, jump);
    22.                 //rb.AddForce(Vector2.up * jump * Time.deltaTime, ForceMode2D.Impulse);
    23.             }
    24.         }
    25.         if (jumpTimeCounter <= 0)
    26.         {
    27.             jumpKeyHeld = false;
    28.         }
    29.  
    30.         if(!isGrounded)
    31.         {
    32.             if(haveDoubleJump)
    33.             {
    34.                 if (!jumpKeyHeld)
    35.                 {
    36.                     numberOfJump = 2;
    37.                 }
    38.             }
    39.         }
    40.  
    41.         if (isGrounded)
    42.         {
    43.             numberOfJump = 0;
    44.             haveDoubleJump = false;
    45.             if (Input.GetButtonDown("Jump"))
    46.             {
    47.                 StartCoroutine("JumpAdd");
    48.                 haveJump = true;
    49.             }
    50.         }
    51.         else
    52.         {
    53.             if (Input.GetButtonDown("Jump"))
    54.             {
    55.                 if (numberOfJump == 0)
    56.                 {
    57.                     numberOfJump = 1;
    58.                 }
    59.                 if (numberOfJump == 1)
    60.                 {
    61.                     haveJump = true;
    62.                     haveDoubleJump = true;
    63.                     Instantiate(jumpCloud, jumpCloudPosition.transform.position, jumpCloudPosition.transform.rotation);
    64.                 }
    65.             }
    66.         }
    67.  
    68.         if (haveJump)
    69.         {
    70.             timeJumpAnim -= 1.5f * Time.deltaTime;
    71.  
    72.             if (timeJumpAnim <= 0)
    73.             {
    74.                 haveJump = false;
    75.             }
    76.         }
    77.         else
    78.         {
    79.             timeJumpAnim = 0.5f;
    80.         }
    81.     }
    Movement script :

    Code (CSharp):
    1.     void MovementFunction()
    2.     {
    3.  
    4.         //CheckCircle for know if is on ground or not
    5.         isGrounded = Physics2D.OverlapBox(groundcheck.position, groundCheckSize, 0, isGround);
    6.         isCollidingWall = Physics2D.OverlapBox(WallCheck.position, WallcheckSize, 0, isWall);
    7.  
    8.         if(isCollidingWall == true && Input.GetAxis("Horizontal") < 0)
    9.         {
    10.             speed = 0f;
    11.         }
    12.         else if (isCollidingWall == true && Input.GetAxis("Horizontal") > 0)
    13.         {
    14.             speed = 0f;
    15.         }
    16.         else
    17.         {
    18.             speed = baseSpeed;
    19.         }
    20.  
    21.         //Movement
    22.         if(Input.GetAxis("Horizontal") > 0)
    23.         {
    24.             transform.Translate(Vector2.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
    25.             //transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    26.             transform.localRotation = new Quaternion(transform.localRotation.x, 0, transform.localRotation.z, transform.localRotation.w);
    27.             dirFacing = new Vector2(0, 0);
    28.         }
    29.         else if(Input.GetAxis("Horizontal") < 0)
    30.         {
    31.             transform.Translate(-Vector2.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
    32.             //transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    33.             transform.localRotation = new Quaternion(transform.localRotation.x, 180, transform.localRotation.z, transform.localRotation.w);
    34.             dirFacing = new Vector2(0, 180);
    35.         }
    36.  
    37.  
    38.         if(oldPos > transform.position.x || oldPos < transform.position.x)
    39.         {
    40.             moveVelocity = 1f;
    41.         }
    42.         else
    43.         {
    44.             moveVelocity = 0f;
    45.         }
    46.  
    47.         oldPos = transform.position.x;
    48.  
    49.         rb.velocity = new Vector2(0f, rb.velocity.y);
    50.  
    51.         //Animation load
    52.         anim.SetFloat("Speed", moveVelocity);
    53.         anim.SetBool("Grounded", isGrounded);
    54.         anim.SetBool("HaveJump", haveJump);
     
    Last edited: Nov 6, 2018
  4. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Nobody ?
     
  5. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Still no clue ?
     
  6. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    24
    Code (CSharp):
    1. StartCoroutine("JumpAdd");
    where is the JumpAdd IEnumerator?
     
  7. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Code (CSharp):
    1.     IEnumerator JumpAdd()
    2.     {
    3.         yield return new WaitForSeconds(0.05f);
    4.         numberOfJump = 1;
    5.     }
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    312
    I'd be a little leery of combining Transform.Translate and RigidBody.velocity but I'm not sure if that's actually your problem or not.
     
  9. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Ive tried with addforce, same issue
     
  10. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    I finnaly resolve that by replacing the :
    Code (CSharp):
    1. rb.velocity = new Vector2(rb.velocity.x, jump);
    By :
    Code (CSharp):
    1. transform.Translate(Vector2.up * jump * Time.deltaTime);
    2.                 rb.velocity = new Vector2(0,0);
    The problem was probably the velocity factor who become higher when moving.

    But now i got another annoying problem, while jumping i can pass through floor or other collider cause its modifying directly the position
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    312
    EDIT: In addition to what I said below, an even-simpler fix might be to try replace your translates with rigidbody.MovePostion. That method is similar to translate, but I think collision will work. I think you need to call it in FixedUpdate, rather than Update. Not sure which you are using currently.

    I think your problem before was mixing different movement methods. Now that you've changed everything to use translate, they are consistent, but transform.translate does not respect collision at all, so instead of changing all of your movement types to translate, you could try Rigidbody.velocity for both horizontal and vertical movement.

    If I were you, though, I'd only set the velocity in just one place to make the code less confusing. I'd use your Jump method to return a vertical movement amount and your MovementFunction to return a horizontal movement amount and then combine them and set the velocity outside of these functions.
     
  12. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    I've done like u said :

    Code (CSharp):
    1.  void MovementFunction()
    2.     {
    3.  
    4.         //CheckCircle for know if is on ground or not
    5.         isGrounded = Physics2D.OverlapBox(groundcheck.position, groundCheckSize, 0, isGround);
    6.         isCollidingWall = Physics2D.OverlapBox(WallCheck.position, WallcheckSize, 0, isWall);
    7.  
    8.         if (isCollidingWall && Input.GetAxis("Horizontal") != 0)
    9.         {
    10.             speed = 0f;
    11.         }
    12.         else
    13.         {
    14.             speed = baseSpeed;
    15.         }
    16.  
    17.         //Movement
    18.         if(Input.GetAxis("Horizontal") > 0)
    19.         {
    20.             speed = baseSpeed;
    21.             //transform.Translate(Vector2.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
    22.             //transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    23.             transform.localRotation = new Quaternion(transform.localRotation.x, 0, transform.localRotation.z, transform.localRotation.w);
    24.             dirFacing = new Vector2(0, 0);
    25.         }
    26.         else if(Input.GetAxis("Horizontal") < 0)
    27.         {
    28.             speed = -baseSpeed;
    29.             //transform.Translate(-Vector2.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
    30.             //transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    31.             transform.localRotation = new Quaternion(transform.localRotation.x, 180, transform.localRotation.z, transform.localRotation.w);
    32.             dirFacing = new Vector2(0, 180);
    33.         }
    34.         else if(Input.GetAxis("Horizontal") == 0)
    35.         {
    36.             speed = 0;
    37.         }
    38.  
    39.  
    40.         if(oldPos > transform.position.x || oldPos < transform.position.x)
    41.         {
    42.             moveVelocity = 1f;
    43.         }
    44.         else
    45.         {
    46.             moveVelocity = 0f;
    47.         }
    48.  
    49.         oldPos = transform.position.x;
    50.  
    51.         //Animation load
    52.         anim.SetFloat("Speed", moveVelocity);
    53.         anim.SetBool("Grounded", isGrounded);
    54.         anim.SetBool("HaveJump", haveJump);
    55.     }
    Code (CSharp):
    1. void Jump()
    2.     {
    3.  
    4.         if (Input.GetButton("Jump"))
    5.         {
    6.             jumpTimeCounter -= Time.deltaTime;
    7.             if (jumpTimeCounter > 0f)
    8.             {
    9.                 jumpKeyHeld = true;
    10.             }
    11.         }
    12.         else if (Input.GetButtonUp("Jump"))
    13.         {
    14.             jumpKeyHeld = false;
    15.             jumpTimeCounter = jumpTime;
    16.         }
    17.  
    18.         if (jumpKeyHeld)
    19.         {
    20.             if (numberOfJump < 2)
    21.             {
    22.                 jump = initalJump;
    23.             }
    24.         }
    25.         else
    26.         {
    27.             jump = 0f;
    28.         }
    29.  
    30.         if (jumpTimeCounter <= 0)
    31.         {
    32.             jumpKeyHeld = false;
    33.         }
    34.  
    35.         if(!isGrounded)
    36.         {
    37.             if(haveDoubleJump)
    38.             {
    39.                 if (!jumpKeyHeld)
    40.                 {
    41.                     numberOfJump = 2;
    42.                 }
    43.             }
    44.         }
    45.  
    46.         if (isGrounded)
    47.         {
    48.             numberOfJump = 0;
    49.             haveDoubleJump = false;
    50.             if (Input.GetButtonDown("Jump"))
    51.             {
    52.                 StartCoroutine("JumpAdd");
    53.                 haveJump = true;
    54.             }
    55.         }
    56.         else
    57.         {
    58.             if (Input.GetButtonDown("Jump"))
    59.             {
    60.                 if (numberOfJump == 0)
    61.                 {
    62.                     numberOfJump = 1;
    63.                 }
    64.                 if (numberOfJump == 1)
    65.                 {
    66.                     haveJump = true;
    67.                     haveDoubleJump = true;
    68.                     Instantiate(jumpCloud, jumpCloudPosition.transform.position, jumpCloudPosition.transform.rotation);
    69.                 }
    70.             }
    71.         }
    72.  
    73.         if (haveJump)
    74.         {
    75.             timeJumpAnim -= 1.5f * Time.deltaTime;
    76.  
    77.             if (timeJumpAnim <= 0)
    78.             {
    79.                 haveJump = false;
    80.             }
    81.         }
    82.         else
    83.         {
    84.             timeJumpAnim = 0.5f;
    85.         }
    86.     }
    87. IEnumerator JumpAdd()
    88.     {
    89.         yield return new WaitForSeconds(0.05f);
    90.         numberOfJump = 1;
    91.     }
    92.  
    And finally applied the velocity :

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         MovementFunction();
    4.  
    5.         rb.velocity = Vector2.ClampMagnitude(rb.velocity, 30f);
    6.         finalResult = new Vector2(speed, rb.velocity.y + jump);
    7.         rb.velocity = finalResult;
    8.     }
    But the problem here, is the double jump is quite low force compare to the first one
     
  13. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    312
    To be honest, I'm having trouble following your jump code, but I'm pretty sure its because you are adding the jump velocity to the player's y velocity and what you really want to do is, when you jump, replace the y velocity with the jump velocity.

    Imagine that your jump initial velocity is 10. When you're on the ground, your velocity.y is 0, so when you press the jump button you move up at a speed of +10. Now if your character is falling, your velocity.y might be -6 or something, so when you press the jump button, your new velocity.y is only +4, so it's much weaker.
     
  14. Stefloper111

    Stefloper111

    Joined:
    Jan 18, 2017
    Posts:
    50
    Yeah my script seem incomprehensible, ive tried a lot of way to code the double jump wich depend on the long or short time by myself, so when i finnaly found this was working ive just let it like that ahah

    And thanks for ur help, that work better this way, i understand now what was wrong