All Questions

Community

Y
Nathan Yungkans

Jump code is different

I'm not sure if I missed a lesson or what happened, but I wasn't able to get the code in this lesson to work properly. I was scanning what else was on screen and I see my func get_direction() is different than yours. I'm not sure where I missed this part in the lessons. Maybe a video ended early?
  • Y
    Nathan Yungkans replied

    I don't know why but using this works:

    var snap: = Vector2.DOWN * 80.0 if direction.y > 0.0 else Vector2.ZERO

  • Nathan Lovato replied

    Slope being a bonus in this edition, the code may look a tiny bit different, but the parts that matter here are the snap vector and the move_and_slide function call.

    I checked earlier videos and the direction function has one difference in the way the code is written, but that doesn't make a difference with the logic. In your case, you should have direction.y be:

    -1.0 if Input.is_action_pressed("jump") and is_on_floor() else 1.0
    

    Which, in this case, should give the same output as

    -Input.get_action_strength("jump") if Input.is_action_pressed("jump") and is_on_floor() else 1.0
    

    That Input.get_action_strength("jump") function call will return a value of 1.0 when you use a keyboard or D-pad on a gamepad, and with the minus sign, you get a value of -1.0 if the player should jump.

    Regarding your snap vector that's working, yes, you can use "if direction.y >= 0.0" as your condition to use the snap Vector.

    A Y direction value that's greater or equal to zero means that the player either did not input a vertical direction (didn't jump) or that the direction vector is pointing down (Y being positive downwards).

  • T
    Fabian Torres replied

    Nathan, as an advise, the bes practice for learning, is understanding what the code is doing, not only copy and memorize the code. Know that you can do the same thing by writing the code in many different ways... if you were understanding you will read both of the codes and see that they say exactly the same.

  • t
    thierry tranchina replied

    When I jump, the direction.y value is -1.0

    So I replace your code with this :

    var snap: = Vector2.DOWN * 50.0 if direction.y != -1.0 else Vector2.ZERO

    And that's why direction.y > 0.0 is working ...