GETTING STARTED IN GODOT – PART 10 – BASIC ENEMY

By Alex K.

 

 

Now you’ve got basic movement. Your challenge for this section is:

 

1)     Add another icon and another script, both called enemy.

 

2)     Have the enemy be coloured red (Visibility > Modulate in the inspector).

 

3)     And have the enemy script constantly move the enemy towards the player.

  

SOLUTION (in case you need it)

 

ENEMY SCRIPT

 

extends Sprite2D

 

@export var speed = 6

 

# Called when the node enters the scene tree for the first time.

func _ready() -> void:

     pass # Replace with function body.

 

# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta: float) -> void:

     var player = $"../Player"

     var playerPosition = player.position

     position = position.move_toward(playerPosition, speed * (delta * 60))