Godot Engine

Godot Engine

A little help with an AI script
Hello,

I am currently working on an AI script for a basic FPS enemy type. I currently have a script that will change the color of the "Enemy" from green to red and have it rotate to look at the player "Warrior" when the player enters an area node on the Enemy. The only hitch is that instead of looking at the player it looks directly away from the player and I can't figure out why. I'm very new to coding so most of this is cobbled ideas from YouTube videos so I don't expect the code to be clean. Here is a sample of the script:

extends KinematicBody

enum {
IDLE
ALERT
}

var state = IDLE
var target

onready var area = $Area

func _on_Area_body_entered(body):
if body.is_in_group("Warrior"):
target = body
state = ALERT

func _on_Area_body_exited(body):
if body.is_in_group("Warrior"):
target = null
state = IDLE

func _process(_delta):
match state:
IDLE:
set_color_green()
ALERT:
set_color_red()
look_at(target.global_transform.origin, Vector3.UP)

func set_color_green():
$Body.get_surface_material(0).set_albedo(Color(0, 1, 0))

func set_color_red():
$Body.get_surface_material(0).set_albedo(Color(1, 0, 0))

The "look_at(target.global_transform.origin, Vector3.UP)" string is what I used to turn the Enemy to look at the player. Any help is appreciated. Thank you.
Legutóbb szerkesztette: HansK; 2020. júl. 15., 5:13
< >
15/5 megjegyzés mutatása
I've not looked deeply into your particular case, but the look_at method will align the *negative* Z axis with the target coordinate.
It's a very common mistake to set up characters so that their "forward" is in the positive Z direction, which leads to situations like yours, where "look_at" makes them appear to point in the exact wrong direction.
simple thing to do would be to use the opposite vector if it's just showing you the opposite side, so use Vector3.DOWN in the look at.
Baroon Valm eredeti hozzászólása:
simple thing to do would be to use the opposite vector if it's just showing you the opposite side, so use Vector3.DOWN in the look at.
That argument just specifies the primary axis to rotate around, so both UP and DOWN would produce the same result IIRC.

One could have it look_at a point in the exact opposite direction to the player, but then you're fighting the engine, and IMO in the long run it's easier to try to adapt your workflow to the engine, not vice versa. I think the simplest answer is just to turn the enemy model/scene around so that its "forward" points in the direction Godot is built to expect.
So I tried Vector3.DOWN and all that changed is that the Enemy rotates upside down AND away from the player. I also rotated so the Enemy is facing -z on its own coordinate plane and it didn't change anything. Even rotated it to 90 degrees and it still faced directly away.
Ok, figured it out. Even though I had the parent node facing -z in the Enemy scene the Area node was still facing +z somehow so it turned the whole thing away.

Thanks guys!
< >
15/5 megjegyzés mutatása
Laponként: 1530 50

Közzétéve: 2020. júl. 15., 5:11
Hozzászólások: 5