Lost with spatial.look_at ; Help :-(

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sxkod

Hi All

I have a world that has an AI patrol agent that fires a projectile when it sees the player. I will include the scene trees and code. I can’t seem to figure out why my projectiles go in every direction except towards player :slight_smile:

If I need to put the entire project [not that big] I can do that too but I think it is some stupid mistake that can easily be spotted with experience.

projectile code:

extends KinematicBody

var target


func _process(delta):
	var ldir=target.global_transform.origin
	look_at(ldir, Vector3(0,1,0))
	move_and_slide(Vector3(0,0,1), Vector3(0,1,0))

agpatrol [the AI agent that instances the projectile and sets the target]

func _fire_at_ply():
	#create a projectile, set it's target. Now the projectile has to do it's own stuff
	if dbg_vuln>200 and ctarget:
		dbg_vuln=0
		dbg_fired=true
		var pi=load("res://projectile.tscn").instance()
		get_parent().add_child(pi)
		pi.set_owner(get_parent())
		pi.global_transform.origin=agp.global_transform.origin+Vector3(0,-2,0)
		pi.target=ctarget

I added the scene trees here on imgur

Thanks everybody
Have a great weekend

:bust_in_silhouette: Reply From: Magso

The projectile isn’t moving forward, it’s moving along the global Z axis.

Change

move_and_slide(Vector3(0,0,1), Vector3(0,1,0))

to

move_and_slide(global_transform.basis.xform(Vector3.FORWARD), Vector3.UP)

Thank you, Thank you, Thank you :-). That worked.

By now, I am nearly bald!!

sxkod | 2019-11-17 17:13

To be honest I don’t know why global_transform.basis.xform() isn’t shortened to local(). I can’t think of any other reason xform would be used.

Magso | 2019-11-17 23:00