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!
How to apply a jump with direction to a character?
- Best Answerset by 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.
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