Search Unity

Problem with jump height

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

  1. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    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):
    1.  
    2.     var h : float = Input.GetAxisRaw ("Horizontal");
    3.     myRigidbody2D.velocity.x = h * speed;
    4.  
    5.     if (canJump) {
    6.         canJump = false;
    7.         myRigidbody2D.velocity.y = jumpForce;
    8.     }
    9.  
    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

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    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.