Sorry but please can you show the codes, on what you wrote? I will check if everything ok.
Here’s everything in case it comes in handy:
var speed = 25
var player_chase = false
var player = null
func _physics_process(_delta):
if player_chase:
var direction = (player.global_position - global_position).normalized()
velocity = direction * speed
if global_position.distance_to(player.global_position) > 10 or global_position.distance_to(player.global_position) < -10:
move_and_slide()
else:
velocity = Vector2.ZERO
func _on_detection_area_body_entered(_body):
player = _body
player_chase = true
func _on_detection_area_body_exited(_body):
player = null
player_chase = false
I am not sure why the error comes, well then remove the line:
if player_chase:
var direction = (player.global_position - global_position).normalized()
velocity = direction * speed
move_and_slide()
else:
velocity = Vector2.ZERO
Error
Do you able to show a screenshot of the screen, while the error comes.
it doesn’t work
I guess, its a tab mistake, you wrongfully removed a tab, do like this:
if player_chase:
var direction = (player.global_position - global_position).normalized()
velocity = direction * speed
if global_position.distance_to(player.global_position) > 10 or global_position.distance_to(player.global_position) < -10: #This is inside it
move_and_slide()
else:
velocity = Vector2.ZERO
I don’t understand if I need to delete all of this or something specific
The enemy stands still
Okay, its my last try:
if player_chase:
var direction = (player.global_position - global_position).normalized()
velocity = direction * speed
move_and_slide()
else:
velocity = Vector2.ZERO
He just slowly walked away from me and hit the wall, He’s hopeless
Last hope maybe:
var direction = (global_position - player.global_position).normalized()
Tell me what happened?
He stands, I enter his line of sight and he moves away from me into the lower wall.
Assuming both your enemy and player are parented to the same node and nothing is ‘top level enabled’ for some reason, your original code is not at fault. I even tested it in a mockup on my own machine, the enemy moves toward the player as intended.
The only thing I can posit is that in your implementation it will cause the enemy to move toward the _body passed by _on_detection_area_body_entered, which we are all assuming to be the player node.
I suspect either it is not, and there is another physics body as a child of the Player, or something is being set to top level = true or offset from the parent in some way.
I UNDERSTOOD WHAT WAS GOING ON! Character body 2d was in one place in the scene, and collision shape and sprite 2d were in another. The enemy always streamed to the character body 2d that was behind the map. The thing is, when I was making the player, I moved the sprite and collision to the middle of the scene, and the character body remained in the upper left corner. Thank you all for spending your time on me