Enemy that jumps to player's position

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

Anyone have any insight on how to code this type of enemy? I’ve been toying with it for a couple of days working with code originally used to make ballistic projectiles like arrows arc to the player’s position but it’s just not quite coming together.
It doesn’t seem like it should be too difficult because I can determine the distance from the enemy’s position to the player’s position but I wonder if there’s an equation or something that I’m unaware of to make it work? It’s a pretty common pattern for enemies in 2D games too, so it’s odd that there isn’t much information out there about implementing them.

:bust_in_silhouette: Reply From: IagoAlmeida

Hey there!

I guess you could use the player position in order to find the direction of the jump, something like this maybe will give you some direction.

    
extends KinematicBody2D
var player

func _ready():
	player = get_node('/root/Node2D/Player')

func jump_to_player():
	var jump_direction = (position - player.position)
	jump_direction.y -= 30 # Adding -30 to Y direction (UP)
	jump_direction.y = clamp(jump_direction.y, -60, 0) # Max JumpForce is -60
	move_and_slide(jump_direction, Vector2.UP)