-1

I am trying to create a function to make enemy chase the player on my game. The player moves on X and Y coordinates and the enemy needs to chase on X and Y coordinates as soon as the player change his coordinate. To do this I am trying the function below but it still does not working on Y coordinate. The X works so well but in Y coordinate enemy does not moving on player position.

How could I do it ?

var _can_attack = false
var _collision_weapon_default_positionX:float
var _raycast_default_rotate_position:float
var _player_ref:Player
var _enemy_direction:Vector2 = Vector2.ZERO 
var target_position

func _physics_process(delta: float) -> void:
    enemy_attack()  
    _flip_me()
    _enemy_move()   
    
func _enemy_move() -> void:
    var x_direction = sign(_player_ref.global_position.x - global_position.x)
    var y_direction = sign(_player_ref.global_position.y - global_position.y)
    _enemy_direction = Vector2(x_direction, y_direction).normalized() * speed
    velocity = _enemy_direction
    move_and_slide()
        
func _flip_me() -> void:
    if(_player_ref.global_position.x > global_position.x):
        animated_sprite.flip_h = false
        collision_shape_2d_attack.position.x = _collision_weapon_default_positionX
        ray_cast_2d.rotation = _raycast_default_rotate_position
    elif(_player_ref.global_position.x < global_position.x):
        animated_sprite.flip_h = true
        collision_shape_2d_attack.position.x = -_collision_weapon_default_positionX
        ray_cast_2d.rotation = -_raycast_default_rotate_position

enter image description here

6
  • 2
    What does "not working on Y coordinate" actually mean? Commented Apr 12 at 5:34
  • 1
    You should also post a complete and reproducible minimal example Commented Apr 12 at 5:35
  • @Mark the enemy does not moving on Y coordinate chasing the player position. Commented Apr 12 at 6:31
  • Then please, show a minimal reproducible example Commented Apr 12 at 7:02
  • 1
    Ok, I don't understand what function your _enemy_direction has, but I guess the enemy velocity could be calculated like this (if you want instant turns instead of the enemy gradually turning towards the player): velocity = (_player_ref.global_position - global_position).normalized() * speed - where speed is a scalar. Commented Apr 12 at 7:47

1 Answer 1

0

Done. I just changed the reference of player(characterbody2D) position to getting the CollisionShape2D of player and did the same on enemy and it works so well. Before did it I was getting the reference of Character2D and it does not working because the Y coordinate was very so far each other. Now it works as I wanted

I did

func _enemy_move() -> void:
    var _player_collision:Vector2 = _player_ref.get_collision_shape_2D_position()
    var x_direction: float = sign(_player_collision.x - collision_shape_2d.global_position.x)
    var y_direction: float = sign(_player_collision.y - collision_shape_2d.global_position.y)
    _enemy_direction = Vector2(x_direction, y_direction)
    velocity = _enemy_direction * speed     
    move_and_slide()
Sign up to request clarification or add additional context in comments.

5 Comments

The actual speed of the enemy will almost always be ~1.41421 * speed and only when the x or y (but not both) diff is zero will it actually be speed. The enemy will also not move directly towards you most of the time. Also, what is get_collision_shape_2D_position? That's not included in what you called a minimal reproducible example.
So, if you want the enemy to actually move at the speed speed and move directly towards the player: _enemy_direction = (_player_collision - collision_shape_2d.global_position).normalized() should do. You can then also remove x_direction and y_direction.
@TedLyngmo I've been created the function get_collision_shape_2D_position on player to return his CollisionShape2D position
It would have been nice to have had the definitions of both player and enemy in the question. Anyway, did you try my suggestion that will make it move at the speed speed and move directly towards the player?
@TedLyngmo yeah! it works. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.