Move towards not working, not sure what's wrong.

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

I want to set the enemy to move to the player if it has been hit by a bullet which fired by player. The enemy is in idle mode, and if been hit and receive a health_changed signal, and runs the following function. It sounds quite straight forward, but my code does not work. Once been hit, the enemy only slide towards the player slightly and then stop moving. I print the player position, and it seems fine. Therefore, it should have something to do with the movement code, do I make any mistake with the codes? Thank you.

func shot_detected(delta):
   var player = $"../Player"
   print (player.global_position)
   var direction = global_position.direction_to(player.global_position)
   enemyVelocity = enemyVelocity.move_toward(direction * 100, 180 * delta)
   move_and_slide(enemyVelocity)

are you calling shot_detected from _physicsprocess() ?

kamel | 2020-07-12 13:51

No, I call the function in func _ready. I think I don’t need to check this every frame, so only when the enemy been hit the first time, when enemy health has changed, a health_change signal fired, and the enemy moves towards the player.

func _ready():
  stats.connect("health_changed", self, "shot_detected")

Idleman | 2020-07-12 14:08

Play around with this, and find out if it is call inside the func _physics_process(delta):it will work. I reset the trigger condition and rearrange the code, now it works.

Idleman | 2020-07-14 06:23

:bust_in_silhouette: Reply From: Idleman

Play around with this, and find out if it is call inside the func _physics_process(delta):it will work. I reset the trigger condition and rearrange the code, now it works.