Add a New Script to the Enemy.
Add a New Script to the Enemy.
Keep the default functions.
Keep the default functions.
We need variables for:
We need variables for:
- move_speed – Enemy movement speed.
- move_direction – Enemy movement direction.
- start_pos – Enemy starting position.
- target_pos – Position enemy will move to.
@export lets us see variables in the inspector
@export lets us see variables in the inspector
In the ready function we can set the start and target positions
In the ready function we can set the start and target positions
In the process function we:
In the process function we:
move enemy to target position
move enemy to target position
when they reach target:
when they reach target:
move back to starting position
move back to starting position
Setting initial values
Setting initial values
Set he move_dir in the inspector to a value of your choice.
Set he move_dir in the inspector to a value of your choice.
-50 would move the enemy up and down by 50 pixels.
-50 would move the enemy up and down by 50 pixels.
Detecting Collisions
Detecting Collisions
Go to the Player node
Go to the Player node
Click on the Node tab
Click on the Node tab
Now click on the Groups section
Now click on the Groups section
Add our Player node to a group
Add our Player node to a group
Return to the Enemy Node
Return to the Enemy Node
Choose the Signals tab
Choose the Signals tab
Add the body_entered signal and connect it.
Add the body_entered signal and connect it.
Now check for a collision with the player in the code
Now check for a collision with the player in the code
Now you can develop your level further by dragging in enemies in strategic places.
Now you can develop your level further by dragging in enemies in strategic places.