All Questions

Community

b
yousef bohadi

Cutting the jump short

After following along until this point in the course, I attempted to combine a small part of the beginner's course with the current code structure; I attempted to add the bit about cutting the jump short (like a Mario jump) to the current code form. I am honestly having trouble in picking the correct location to implement the code. I believe that the correct location for it is under the physics_process function in the Move.gd script. The code however needs a KinematicBody2D as an extension, while the Move.gd script extends a State (which extends a Node), so I keep getting an error when writing the start of the short jump code under the Move physics_process:

	var is_jump_interrupted: = is_action_just_released("jump") and velocity < 0.0

The error says: The method "is_action_just_released" isn't declared in the current class


Any pointers would be helpful

  • Nathan Lovato replied
    Solution

    First, is_action_just_released is a method of the Input class. So you need to write Input.is_action_just_released().

    Then I would rather put jump interruption in the Air state, and change the jump logic to not just apply an impulse, as the jump is part of the Air movement. I might also split the Air state into Jump and Fall if necessary, after updating the code. It depends on how complex it gets.

  • Nathan Lovato replied
    Solution

    For more information, Move is the parent of Idle, Run, and Air. You want to limit the code there as much as possible to what is common to Idle, Run, Air, and movement states in general.

  • b
    yousef bohadi replied

    I ended up implementing the short jump in the move script (I honestly tried for a while to implement it in the Air script, but I still lack the experience to do so)

    In the Move.gd script, I added the is_jump_interrupted variable to the physics_process function:

    func physics_process(delta):
    	var is_jump_interrupted: bool = Input.is_action_just_released("jump") and velocity.y < 0.0
    	velocity = calculate_velocity(velocity, max_speed, acceleration, delta, get_move_direction(), is_jump_interrupted)
    

    then in the calculate_velocity function I added the new variable + the last "if" statement:

    static func calculate_velocity(
    	old_velocity: Vector2, 
    	max_speed: Vector2, 
    	acceleration: Vector2, 
    	delta: float, 
    	move_direction: Vector2, 
    	is_jump_interrupted: bool
    ) -> Vector2:
    	var new_velocity: = old_velocity
    	
    	new_velocity += move_direction * acceleration * delta
    	new_velocity.x = clamp(new_velocity.x, -max_speed.x, max_speed.x)
    	new_velocity.y = clamp(new_velocity.y, -max_speed.y, max_speed.y)
    #	if statment below for interrupting the jump:
    	if is_jump_interrupted:
    		new_velocity.y = 0.0
    	
    	return new_velocity
    
    

    lastly, in the Air.gd script I added the "false" at the end of the calculate_jump_velocity function:

    func calculate_jump_velocity(impulse: float = 0.0) -> Vector2:
    	var move: State = get_parent()
    	return move.calculate_velocity(
    		move.velocity, 
    		move.max_speed, 
    		Vector2(0.0, impulse), 
    		1.0, 
    		Vector2.UP, 
    		false
    	)
    

    And to my surprise, that worked :-)


  • Nathan Lovato replied
    Solution

    Good job. Yes it works. Moving this to the air state would require more changes in the code, it's a bit trickier.

  • b
    yousef bohadi replied

    A challenge for another day perhaps ;-)