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
onready var player = get_parent().get_node(“Player”)
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)
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?
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.
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.
export var speed = 180
onready var player = get_parent().get_node(“Player”)
var player_position
func _ready():
# 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
var angle_deg = rad2deg(angle_rad)
# 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