How to launch a kinematic body in a certain direction?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CosmicLife

I wan to have a system similar to Ori’s slam ability or that one shovel knight DLC I can’t remember the name of, where the player launches toward(Through) the nearest enemy/special obstacle. Here is what the function looks like currently

Code starts here, the code detector kind of sucks.
VV
func dash_slash():

var enemies = get_tree().get_nodes_in_group("Enemies")
var nearest_enemy = enemies[0]
for enemy in enemies:
	if enemy.global_position.distance_to(player_pos) < nearest_enemy.global_position.distance_to(player_pos):
		nearest_enemy = enemy

dash_slash_apply = get_angle_to(nearest_enemy.global_position) * DASHSLASHFORCE

#How do I make this a vector 2D?

move_and_slide(dash_slash_apply)

As you may be able to see I am able to find the position of the nearest enemy(hopefully that works at least though I don’t really know), all I want to do is launch the player toward that point. They are a kinematic body. Anything that makes the player move toward that point at high speed would work too, it doesn’t have to be an impulse.

:bust_in_silhouette: Reply From: Juxxec

You simply subtract your character’s position from the enemy’s position:

var linear_velocity = Vector2()
var dashing = true

func _physics_process(delta):
    if not dashing:
        linear_velocity.y += GRAVITY * delta 

        if is_on_floor():
            linear_velocity.y = 0
    else:
        # This will make your dash lose speed over time
        linear_velocity = lerp(linear_velocity, 0.0, delta)
        # Stop dashing when we are too slow
        dashing = linear_velocity.length() > 0.1
   
   linear_velocity = move_and_slide(linear_velocity)

func dash_slash():
    # Avoid dash spamming
    if dashing:
         return
    dashing = true

    var enemies = get_tree().get_nodes_in_group("Enemies")
    var nearest_enemy = enemies[0]
    var nearest_enemy_pos = enemies[0].global_position.distance_to(player_pos)

    for enemy in enemies:
        var enemy_pos = enemy.global_position
        if enemy_pos.distance_to(player_pos) < nearest_enemy_pos:
            nearest_enemy_pos = enemy_pos
            nearest_enemy = enemy

    # Now you have a vector to use
    dash_slash_apply = (nearest_enemy_pos - player_pos) * DASHSLASHFORCE

    move_and_slide(dash_slash_apply)

I modifed your code a bit to be a true dash abillity, but the answer to your question is:

dash_slash_apply = (nearest_enemy_pos - player_pos) * DASHSLASHFORCE

Thanks so much! I really appreciate it especially since it is a “simple” question that only I seem to have had trouble with, so I couldn’t find the answers online. Thanks!

Edit: There seems to be a problem with

linear_velocity = lerp(linear_velocity, 0.0, delta)

It says it can’t lerp between a Vector 2D and it needs a float. Did I do something wrong?

CosmicLife | 2022-11-23 19:01

Sorry about that. The lerp should be:

linear_velocity = lerp(linear_velocity, Vector2.ZERO, delta)

Have fun!

Juxxec | 2022-12-01 09:48

I’m really sorry but I have been trying to get your code to work for days and it seems to be riddled with errors. I hate to be so negative but it seems like you didn’t test your code at all before handing it off to me. I will list a couple of the problems I am having and will be very thankful if you help me fix them.

  1. If I use your edited code the program says that

dash_slash_apply = (nearest_enemy_pos - player_pos) * DASHSLASHFORCE

is creating an error because a float and a Vector2D can’t use the - operand.
2. If I use your non edited code and use the simple version you gave me it just flings my character off to the bottom right no matter where I stand.

I hope you don’t feel like I’m accusing you of anything and I really appreciate the response but it’s so frustrating that you gave me code that clearly doesn’t work and hasn’t been tested. I hope we can figure this out together.

CosmicLife | 2022-12-14 23:36