I am in the process of developing a 2D enemy that emulates the behavior of a slime, specifically jumping towards the player. I have been able to successfully implement this feature. However, upon further testing, I have observed that while the first jump executes in accordance with the intended behavior, subsequent jumps inexplicably occur in a direction opposite to the player's location. I would appreciate if you could help me solve this problem.
Jump Code here:
if is_on_floor():
var player_pos = get_node("../Player").get_global_position()
var self_pos = get_global_position()
var jump_distance = (player_pos - self_pos).normalized()
var distance_to_player = player_pos.distance_to(self_pos)
var horizontal_speed = min_speed + (distance_to_player / max_distance) * (max_speed - min_speed)
horizontal_speed = min(max_speed, max(min_speed, horizontal_speed))
velocity = jump_distance
velocity.x *= horizontal_speed * direction
velocity.y = -jumpmove_height #-jump_height
velocity = move_and_slide(velocity, Vector2.UP)
$Position2D/SlimeSkin/AnimationPlayer.play("SlimePrototype Attack")
$Jump_Cooldown.start()
print("Do Jump")
All Code here: extends KinematicBody2D
var speed = 20
var min_speed = 0 #min_speed for jump horizontal speed
var max_speed = 250 #max_speed for jump horizontal speed
var max_distance = 300 #max distance for jump distance
var direction = 1
export var jumpmove_height : float
export var jump_time_to_peak : float
export var jump_time_to_descent : float
onready var jump_velocity : float = ((2.0 * jumpmove_height) / jump_time_to_peak) * -1.0
onready var jump_gravity : float = ((-2.0 * jumpmove_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
onready var fall_gravity : float = ((-2.0 * jumpmove_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0
var velocity = Vector2.ZERO
var is_idle = true
func _ready():
is_idle = true
$Idle_Timer.start()
pass
func get_gravity() -> float:
return jump_gravity if velocity.y < 0.0 else fall_gravity
func _physics_process(delta):
if velocity.x>0:
$Position2D.scale.x=1
elif velocity.x<0:
$Position2D.scale.x=-1
if is_on_wall() or not $Position2D/FloorRay.is_colliding() and is_on_floor():
direction = direction * -1
velocity.y += get_gravity() * delta
if is_on_floor():
velocity.x = 0
velocity = move_and_slide(velocity, Vector2.UP)
if is_idle:
velocity.x = speed * direction
if is_on_wall() or not $Position2D/FloorRay.is_colliding() and is_on_floor():
direction = direction * -1
velocity = move_and_slide(velocity, Vector2.UP)
$Position2D/SlimeSkin/AnimationPlayer.play("SlimePrototype Idle")
func _on_Timer_timeout():
if is_on_floor():
var player_pos = get_node("../Player").get_global_position()
var self_pos = get_global_position()
var jump_distance = (player_pos - self_pos).normalized()
var distance_to_player = player_pos.distance_to(self_pos)
var horizontal_speed = min_speed + (distance_to_player / max_distance) * (max_speed - min_speed)
horizontal_speed = min(max_speed, max(min_speed, horizontal_speed))
velocity = jump_distance
velocity.x *= horizontal_speed * direction
velocity.y = -jumpmove_height #-jump_height
velocity = move_and_slide(velocity, Vector2.UP)
$Position2D/SlimeSkin/AnimationPlayer.play("SlimePrototype Attack")
$Jump_Cooldown.start()
print("Do Jump")
func _on_Idle_Timer_timeout():
is_idle = false
$Timer.start()
$Position2D/SlimeSkin/AnimationPlayer.play("SlimePrototype Charge")
print("IDLE TIMER TIMEOUT")
pass # Replace with function body.
func _on_Jump_Cooldown_timeout():
yield(get_tree().create_timer(0.5), "timeout")
is_idle = true
pass # Replace with function body.