Hi,
I'm working on a 2d platform game. The game has spring that'll push the player to jump high. I do have it working now but the problem is, the spring is not always placed on a flat platform. It could be placed on a slope and the angle can be completely random. It doesn't quite make sense to give only a y velocity in this case. How do I make the character and spring interaction more reasonable? Thanks!

  • ohpoloha

    No, that looks like you're rotating your character. Not what you want. You want to rotate the launch vector. Here is an example:

    extends Area2D
    
    @export var launch_speed = -800.0
    
    func _ready() -> void:
    	body_entered.connect(_body_entered)
    
    func _body_entered(body) -> void:
    	body = body as CharacterBody2D
    	if body:
    		body.velocity = Vector2(0.0, launch_speed).rotated(rotation)

    A second thing to note, if you're using the script Godot creates for you automatically for a CharacterBody2D, then you need to change it so that it doesn't dampen your character while you're in the air. Otherwise your horizontal velocity won't matter!

    You can change

    	if direction:
    		velocity.x = direction * SPEED
    	else:
    		velocity.x = move_toward(velocity.x, 0, SPEED)

    to

    	if direction:
    		velocity.x = direction * SPEED
    	elif is_on_floor():
    		velocity.x = move_toward(velocity.x, 0, SPEED)

    to avoid that.

Take your y velocity you normally add to the player and make it a Vector2D(0.0, y). Then rotate it the same amount your spring is rotated before applying it to your character.

    award it doesn't work 🤔 . my character is still jumping straight up. This is what I have for example:

    func jump():
    	rotate(45)
    	velocity = Vector2(0.0, -600)

      ohpoloha You need to rotate the velocity vector, not the character node.

      velocity = Vector2(0.0, -600.0).rotated(deg_to_rad(45))

        ohpoloha

        No, that looks like you're rotating your character. Not what you want. You want to rotate the launch vector. Here is an example:

        extends Area2D
        
        @export var launch_speed = -800.0
        
        func _ready() -> void:
        	body_entered.connect(_body_entered)
        
        func _body_entered(body) -> void:
        	body = body as CharacterBody2D
        	if body:
        		body.velocity = Vector2(0.0, launch_speed).rotated(rotation)

        A second thing to note, if you're using the script Godot creates for you automatically for a CharacterBody2D, then you need to change it so that it doesn't dampen your character while you're in the air. Otherwise your horizontal velocity won't matter!

        You can change

        	if direction:
        		velocity.x = direction * SPEED
        	else:
        		velocity.x = move_toward(velocity.x, 0, SPEED)

        to

        	if direction:
        		velocity.x = direction * SPEED
        	elif is_on_floor():
        		velocity.x = move_toward(velocity.x, 0, SPEED)

        to avoid that.

          xyz This is so much fun. thank you very much for the help!

          award should've refreshed the page first. spent some time on debugging why the char is jumping as smooth. elif is_on_floor(): indeed made the magic happen. thanks again!

          9 months later

          @award hey ive tried this but now my character is keeping x momentum when they jump and i dont really know how to fix this
          so like when you move left they will still move left and vice versa