How to get transform of a object

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

I am trying to get the transform of an object so that I can use the look_at function so that the kinematic body is looking at it the only problem is that I am having trouble getting the transform of the player what I was trying to do was

target_player = ray_collide

target_player.global_transform.origin

to get the transform but that does not work as I get the

Invalid get index 'global_transform' (on base: 'Nil').

I would just like to know if there is any way that I can ger a Vector3 for the Look_at() so that the enemy will look at the player
(This is also multiplayer if that is of any consequence)

:bust_in_silhouette: Reply From: Wakatta

Your target_player var is not properly set.
So ensure that its nodepath resolves correctly

I set it as the collision of the ray cast which only collides with the player and only tries to find the transform if the ray cast has a collision

Shazelz | 2021-03-25 20:10

Odd. Its obviously returning null. How is the code for your Raycast setup?

Wakatta | 2021-03-25 20:26

	if ray_collide != null:
	   if ray_collide_.is_in_group("player") == true:
		  target_player = ray_collide

and the ray cast is returning the kinematic body the only thing is when I try to get the translation of it for the look_at() it doesn’t work

Another problem has also come up with this

	look_at(possible_players[0].get_translation(), Vector3(0, 0, 0))

I need to get the origin for the look_at() but nothing seems to be working
(possible players is just a array full of the players)

Shazelz | 2021-03-25 20:40

Sorry to have been so vague before. I meant how is your code setup that captures the collision. Because usually you’d do

If $Raycast.is_colliding():
    target_player = $Raycast.get_collider()
    if target_player.is_in_group("player"):
        look_at(target_player.global_transform.origin, Vector2.UP)

But without knowing what your “ray_collide” var actually is/does then that’s as far as my olive branch can extend because all of your code looks legit on the surface

In your last example change Vector3(0, 0, 0) to Vector3.UP

Wakatta | 2021-03-26 23:46