Godot Version
4.4.1
Question
`I just started the next step in my game which is making a simple enemy but I hit a roadblock on how to make the enemy move and use the same path finding as the player
Player’s movement code:
extends Node2D
@onready var tile_map: TileMapLayer = $"../TileMap"
@onready var sprite_2d: Sprite2D = $CharacterBody2D/Sprite2D
@onready var animation_player: AnimationPlayer = $CharacterBody2D/AnimationPlayer
@onready var animation_tree: AnimationTree = $CharacterBody2D/AnimationTree
@onready var options_in: Button = $UI_buttons/Options_in
@onready var inventory_in: Button = $UI_buttons/Inventory_in
var astar_grid: AStarGrid2D
var current_id_path: Array[Vector2i]
var current_point_path: PackedVector2Array
var target_position: Vector2
var char_moving: bool
var direction: Vector2
var options_screen: Node = null
var inventory_screen: Node = null
var speed : float = 100
var velocity : Vector2 = Vector2()
var input_locked := false
func _ready():
animation_tree.active = true
astar_grid = AStarGrid2D.new()
astar_grid.region = tile_map.get_used_rect()
astar_grid.cell_size = Vector2(32, 32)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.update()
for x in tile_map.get_used_rect().size.x:
for y in tile_map.get_used_rect().size.y:
var tile_position = Vector2i(
x+tile_map.get_used_rect().position.x,
y+tile_map.get_used_rect().position.y
)
var tile_data = tile_map.get_cell_tile_data(tile_position)
if tile_position == null or tile_data.get_custom_data("Walkable") == false:
astar_grid.set_point_solid(tile_position)
target_position = Vector2(16,16)
if global_position == target_position:
direction = Vector2(0,-1)
animation_tree["parameters/conditions/idle"] = true
animation_tree["parameters/conditions/is_moving"] = false
animation_tree["parameters/idle/blend_position"] = direction
func _process(_delta):
update_animation_parameters()
func _unhandled_input(event):
if event.is_action_pressed("move") == false:
return
var id_path
if char_moving:
id_path = astar_grid.get_id_path(
tile_map.local_to_map(target_position),
tile_map.local_to_map(get_global_mouse_position())
)
else:
id_path = astar_grid.get_id_path(
tile_map.local_to_map(global_position),
tile_map.local_to_map(get_global_mouse_position())
)
if id_path.is_empty() == false:
current_id_path = id_path
current_point_path = astar_grid.get_point_path(
tile_map.local_to_map(target_position),
tile_map.local_to_map(get_global_mouse_position())
)
for i in current_id_path.size():
current_point_path[i] = current_point_path[i] + Vector2(16, 16)
func _physics_process(_delta: float) -> void:
if current_id_path.is_empty():
return
if not char_moving:
target_position = tile_map.map_to_local(current_id_path.front())
char_moving = true
var walking_direction = target_position - global_position
velocity = walking_direction.normalized() * speed * _delta
global_position = global_position.move_toward(target_position, velocity.length())
if global_position.distance_to(target_position) < 1:
current_id_path.pop_front()
if not current_id_path.is_empty():
target_position = tile_map.map_to_local(current_id_path.front())
else:
char_moving = false
Enemy’s code:
extends CharacterBody2D
@onready var tile_map: TileMapLayer = $"../../TileMap"
@onready var detection_area: Area2D = $DetectionArea
var astar_grid: AStarGrid2D
var current_id_path: Array[Vector2i]
var current_point_path: PackedVector2Array
var target_position: Vector2
var enemy_moving: bool = false
var speed: float = 100.0
var player: Node2D = null
func _on_DetectionArea_body_entered(body: Node2D) -> void:
pass
func _on_DetectionArea_body_exited(body: Node2D) -> void:
pass
Not asking for much but to just look over and give me a few tips on how should I do stuff like this and get me on the right path. Thanks
`