extends CharacterBody3D
@export var speed: float = 300
@export var gravity = 200
@export var target_path: NodePath
var target: Node3D
func _ready():
# Replace "$Player" with the correct path to your player node
target = get_node(target_path)
if target == null:
print("Player not found!")
func _physics_process(_delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * _delta
if target == null:
return
var direction = target.global_position - global_position
direction.y = 0 # horizontal movement only
if direction.length() == 0:
return
var target_pos = $"../Player".global_position
target_pos.y = global_position.y
look_at(position + velocity)
rotate_y(PI)
direction = direction.normalized()
# Maintain current vertical velocity if needed
velocity = direction * speed + Vector3(0, velocity.y, 0)
print("Direction: ", direction, " Velocity: ", velocity)
move_and_slide()
look_at() expects a point in global coordinates, so try look_at(global_position + velocity).
I don’t know why you set up your character to face the ground though? Your origin points in the scene tree seem to be off as well. Try to clean up the scene.
You have hundreds of errors in the Debugger - maybe look into fixing these first.
Also, what does the AnimationPlayer do? Isn’t there any animation that affects the rotation?
But it’s not doing what you want it to do. And fixing the errors is the first thing you should consider.
What the error tells you is that you’re trying to make your node to look at the same position it currently stands on, which is impossible. Try moving your look_at() to be right before the move_and_slide(), after you have calculated the velocity. You can also make an if statement check to see if the velocity is not approximately zero before you perform the look_at().
OK, that’s good.
You just need to properly rotate the MeshInstance3D node that holds your 3D model in your character scene to have it stand up. Then it should look correct.
ERROR: res://Script/EnemyChase.gd:29 - Parse Error: Identifier "Player" not declared in the current scope.
ERROR: res://Script/EnemyChase.gd:29 - Parse Error: Identifier "player" not declared in the current scope.
ERROR: res://Script/EnemyChase.gd:29 - Parse Error: No constructor of "Vector3" matches the signature "Vector3(Variant, Variant)".
The errors say, that you don’t have any variable named “Player” and “player” in your script or you tried to access a variable that is inside a function from outside (another function for example).
The last error is telling you, that Vector3 needs 3 values, but you are setting only 2.
I guess it should be this: