# Help please with enemy AI

3

### Question

I have an enemy plane and i wont it to follow my player and rotate with it, but i cant make it to rotate properly.

My enemy code.

extends KinematicBody2D

# Movement speed

export var speed = 100
var player_position
var target_position

func _physics_process(delta):

``````# Set player_position to the position of the player node
player_position = player.position
# Calculate the target position
target_position = (player_position - position).normalized()

# Check if the enemy is in a 3px range of the player, if not move to the target position
if position.distance_to(player_position) > 3:
move_and_slide(target_position * speed)
look_at(player_position)
``````

If they have the same parent, you can assign the player’s rotation property to the plane’s rotation property.

1 Like

Your question is a little confusing but I think you’re asking how to have your enemy always point or rotate towards the player, is that correct?

If that’s the case then using look_at(player.global_position) under physics process should allow you to do this pretty easily.

2 Likes

I was using it, but my plane doesn’t rotate properly.I want the nose of the plane to look towards the player but it turns sideways.

Is the image of the plane pointing to the right?

1 Like

thats what i get a plane is not facing my player properly

can you upload the plane image directly? The math for rotation assumes you base image is pointing right, based on this image maybe it’s facing left instead?

1 Like

As gertkeno said, your plane image must be drawn so it is facing the right. If you’re not familiar with the unit circle, I recommend learning about it. Zero degrees is to the right, and Godot’s rotation math depends on that. If your image does not face right at zero degrees, your rotations will look wrong.

2 Likes

I made it! I just add to the code + 90 degrees when he was rotating and it worked !

1 Like

Thanks for helping.

1 Like

Yes, it will work. However, you will be stuck always having to offset your rotations by 90 degrees throughout the rest of your game. You will tire of that at some point and want to fix it. The longer you wait to fix it, the harder and more overwhelming it will become. I suggest fixing it now when you’re early in your game’s development.

You may also repeat this mistake in other areas of your game, and have to implement the same hack.

extends KinematicBody2D

# Movement speed

export var speed = 180
var player_position

# Initialize player_position with the player’s current position
player_position = player.position

func _physics_process(delta):
# Update player_position with the player’s current position
player_position = player.position

``````# Check the distance between the current position and the player's position
if position.distance_to(player_position) > 3:
# Calculate the angle in radians
var angle_rad = (player_position - position).angle()
# Convert the angle to degrees
# Apply the -30 degrees offset
var final_angle_deg = angle_deg + 90
# Set the node’s rotation_degrees to the calculated angle in degrees
rotation_degrees = final_angle_deg

# Move the plane towards the player
var direction = (player_position - position).normalized()
position += direction * speed * delta
``````

Thats my code

Gj on getting this sorted. It’s a good learning opportunity.

Unless you’re wanting to use the other values you could have one equation for this.

typing from my phone so my apologies for any typos.