Chasing the player

Help me how to chase the player, that is, so that the mod moves after the player when he enters his area2d zone

a simple way is just move forward combined with look_at

It took too long to explain, so I asked Copilot to provide an example, you can use it:

  1. Set Up the Player:

    • Create a new 2D scene and add a KinematicBody2D node. Name it Player.

    • Add a Sprite node as a child of Player to represent the player’s visual.

    • Add a CollisionShape2D node to define the player’s collision area.

    • Attach a script to the Player node to handle movement:

      extends KinematicBody2D
      
      const SPEED = 200
      
      func _physics_process(delta):
          var motion = Vector2.ZERO
          if Input.is_action_pressed("ui_right"):
              motion.x += SPEED
          if Input.is_action_pressed("ui_left"):
              motion.x -= SPEED
          if Input.is_action_pressed("ui_down"):
              motion.y += SPEED
          if Input.is_action_pressed("ui_up"):
              motion.y -= SPEED
          motion = move_and_slide(motion)
      
  2. Create the Enemy:

    • Create a new 2D scene and add a KinematicBody2D node. Name it Enemy.
    • Add a Sprite node as a child of Enemy to represent the enemy’s visual.
    • Add a CollisionShape2D node to define the enemy’s collision area.
    • Add an Area2D node as a child of Enemy to detect the player.
    • Add a CollisionShape2D node as a child of Area2D to define the detection range.
  3. Script the Enemy:

    • Attach a script to the Enemy node to handle the chasing behavior:

      extends KinematicBody2D
      
      const SPEED = 100
      var player = null
      
      func _ready():
          $Area2D.connect("body_entered", self, "_on_Area2D_body_entered")
          $Area2D.connect("body_exited", self, "_on_Area2D_body_exited")
      
      func _on_Area2D_body_entered(body):
          if body.name == "Player":
              player = body
      
      func _on_Area2D_body_exited(body):
          if body == player:
              player = null
      
      func _physics_process(delta):
          if player:
              var direction = (player.global_position - global_position).normalized()
              move_and_slide(direction * SPEED)
      
  4. Combine the Scenes:

    • Add both the Player and Enemy scenes to your main scene.
    • Position them appropriately.

Now, when the player enters the enemy’s detection range, the enemy will start chasing the player. If the player leaves the range, the enemy will stop chasing.

Sorry, that was a mistake, you don’t need the contents of the _ready() function

If I’m not mistaken, the global position is obtained not from the script of the character or mob, but from the global one (I deleted what you wrote, but it didn’t even move)

Maybe this will work:

extends KinematicBody2D

const SPEED = 100
var player = null

func _on_Area2D_body_entered(body):
    if body.name == "Players":
        player = body

func _on_Area2D_body_exited(body):
    if body == player:
        player = null

func _physics_process(delta):
    if player:
        var direction = (player.global_position - global_position).normalized()
        move_and_slide(direction * SPEED)

I think the problem is with identifying your player, it should be called Players (in the previous code it was Player but I noticed you named the player as Players)

im not good at english , you want to enemy moves after your charachter ?

this code is for 2d games

func on_body_entered(body):
if posision.x>body.posision.x:
vector2.x= an number
elif posision.x<body.posision.x:
vector2.x= an number

Your code isn’t working because Copilot’s information is outdated. This is a thread about Godot 4, where the KinematicBody2D was replaced with CharacterBody2D. Also move_and_slide() doesn’t seem to take arguments in Godot 4; I don’t know if it did in previous versions but it certainly doesn’t now.

Even after tweaking your code to fix the errors and change the names to ones relevant in my project, it still doesn’t work for me, your code is not tracking my player. Is it possible for you to create a scene and demonstrate this method of tracking? I’m new to Godot and having similar difficulties to the OP.

use a navigation 2d.
so you can also implement avoidation