How to get enemy face my player?

Godot Version

Godot 4.5 stable version

Im trying to make the enemy character to face me while walking toward, however, its facing the wrong way. I don’t know what to do with “look at”.

This was like this at the beginning of the video, facing downward, and in the game, its facing slightly off of the player.

Here’s the video.

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()

Generally it’s look_at(target)

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.

Like this ?

Same result

The script already has Target Path.

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?

I’ve animated my character in Blender 5 vers and exported FBX from there.

I may have errors, but it still playing the game.
Im not fluent in coding, so I dont understand the error mean. It still have something to do with


look_at(global_position + velocity)


It got 2 animations; walking, attack, but I didn’t added Cult’sBody_001Action because it dont have an animation. Here’s the video.

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().

1 Like

Like this?

    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)
	
	look_at(global_position + velocity)
	move_and_slide()

And where I calculate the velocity?

Yeah, like this. Move the rotate_y() with it.
Then see if that fixes anything.

Man, I haven’t laughed for a while.
The character possibly dont want to get up and attack the player, but lol.

Here the video.

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.

Uh… I got is CollisionShape 3D…

You have your model under the OccultTeenage something Node3D, so you can rotate this one.

It won’t let me rotate em.
Here the video.

It’s super granny :rofl:

1 Like

Just found the video, about Gscript. I’ve skipped ahead to 14:13

I did tried Nav agent and region before, but some reasons they aren’t working in Godot 4.5, so Im trying other way like this one.

look_at(Vector3(player.global_position.y, player.global_position.z), Vector3.UP)

Now I got a error about this;

  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)".

I don’t understand this.

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:

look_at(player_global_position , Vector3.UP)```