Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Problem with jump height

Discussion in 'Physics' started by justJack, Feb 21, 2015.

  1. justJack


    Feb 5, 2015
    Hello everyone,

    I am having some kind of bug with my jump. I know it is not, by the way...

    Well, when I am grounded and press X, I jump, and in this is case, everything is fine.
    But when I am walking and jump, the jump height is serevely reduced based on the velocity (x axis). Also, if I release the key responsible for movement while in mid jump, my character is able to reach the expected and maximum height, which is very strange as the character is suddenly impulsed.

    Right now,
    I am manipulating each axis separately, like this:
    Code (JavaScript):
    2.     var h : float = Input.GetAxisRaw ("Horizontal");
    3.     myRigidbody2D.velocity.x = h * speed;
    5.     if (canJump) {
    6.         canJump = false;
    7.         myRigidbody2D.velocity.y = jumpForce;
    8.     }
    The reason why I am changing velocity like this and not adding force instead is because it is an arcade platformer and every movement must be fast and sharp. Nonetheless, I have tried adding force instead and the problem persisted, so...

    Any help is very appreciated.

    Thank you in advance.

    - edit: the boolean canJump is being treated on the Update function. This is just a piece of code. The player is able to jump when the X key is pressed, and is grounded. Ir order to be grounded, the player must be colliding with the environment, and the velocity on the y axis must be 0. I did this ir order to avoid that problem when the player might stick to something, like a wall, when falling and walking toward it.
    Last edited: Feb 21, 2015
  2. justJack


    Feb 5, 2015
    Just solved my question.

    I omitted the FlipHorizontally function from the code because I could not imagine any interaction between it and my problem... Well, I am pretty sure that scaling x to 1 or -1 was messing with the simulation somehow.

    Anyway, before solving the problem, it was something like:

    Code (JavaScript):
    1.     if (h < 0) {
    2.         myTransform.localScale.x = -1;
    3.     }
    4.     else if (h > 0) {
    5.         myTransform.localScale.x = 1;  
    6.     }
    I just added a boolean (isFacingRight) ir order to avoid changing the scale whenever FixedUpdate is called. Now it is scaled only if the player is facing another direction.
    Nanako likes this.