Godot Version
4.3
Question
As I move my player the enemy does not update the players position to look at it.
enemy.gd
extends CharacterBody2D
@export var player_scene : PackedScene
var player = load('res://player.tscn').instantiate()
func _physics_process(_delta):
print(player)
if player:
print("Player position: ", player.position)
look_at(player.position)
else:
print("Error: Player node not found.")
player_movement.gd
extends CharacterBody2D
var _rotation_speed : float = TAU * 2
var _theta : float
var _direction : Vector2
func move(direction : Vector2):
_direction = direction
func _physics_process(delta : float):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
input_vector = input_vector.normalized()
if _direction:
_theta = wrapf(atan2(_direction.y, _direction.x) - rotation, -PI, PI)
rotation += clamp(_rotation_speed * delta, 0, abs(_theta)) * sign(_theta)
if input_vector:
velocity = input_vector * 300
rotation = lerp_angle(rotation, atan2(-velocity.x, -velocity.y), delta * 75.0)
else:
velocity = input_vector
move_and_slide()
I’m not 100% sure but, my thoughts are this:
- Instantiating the player in the enemy script makes it local to that enemy. It’s basically instantiated on top, same orientation, etc. as the enemy.
- I believe you have to add the player to the scene so that it can be used. Right now, the player is sitting in the player variable idling. The player variable won’t trigger errors because it’s not null/other but, it also doesn’t exist in the game(scene tree) to be looked at by the enemy.
- I think signals might make this better. It’ll keep the enemy and the player separate but, interactable. I think it’s called decoupling.
- Running the logic to look at the player, every frame will hinder performance.
Questions
- Are we sure that we’re looking at the correct player and not another instantiated player that was instantiated by the enemy?
- Can you confirm if the position of the player changes when attempting to move them? Separate from the enemy’s position? Does the enemy face one direction and one direction only?
- Can you run debug tests to gather more trouble shooting data/narrow down the potential issue?
Hope this helps.
Yes, it faces only to where the player originally was (925, 254) and doesn’t change direction.
How can I do this?
I Believe your issue is how you’re dealing with the creation of your player and communication between player and enemy.
- Read the manual and do some research. YouTube is a great resource. Search, “Godot 4.x signals” on YouTube, to get started.
- Research signals(connect, emit), autoloads/singletons as well as scope…amongst other topics.
Also, If all else fails, restart from scratch. Working out the logic, one step at a time. This can help narrow down issues, learn how things work and more.
1 Like
Your var player
isn’t a player in the scene, you’ve created a new player that isn’t a child of any node. Maybe you meant to export a node?
@export var player: CharacterBody2D
2 Likes