Godot 4
Question
I am new to Godot and I ran into a problem. I am trying to add a mob in my world scene that goes towards the player when the player is within the mobs area. I was able to make the mob go towards the player when the player is standing on the left side of the mob but it doesn’t work when you stand on the right side of the mob, it will walk away from player rather than go towards it. I attached the script for the mob.
extends CharacterBody2D
var SPEED = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player
var chase = false
func _physics_process(delta):
#Gravity for frog
velocity.y += gravity * delta
if chase == true:
get_node("AnimatedSprite2D").play("Jump")
player = get_node("../../Player/Player")
var direction = (player.position - self.position).normalized()
velocity.x = direction.x * SPEED
if direction.x > 0:
get_node("AnimatedSprite2D").flip_h = true
print("right")
else:
get_node("AnimatedSprite2D").flip_h = false
print("left")
else:
get_node("AnimatedSprite2D").play("Idle")
velocity.x = 0
move_and_slide()
func _on_player_detection_body_entered(body):
if body.name == "Player":
chase = true
func _on_player_detection_body_exited(body):
if body.name == "Player":
chase = false