How to know kinematic's body position in 3D??

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

Hi,
I have tried many way but i Don’t understand how really get_position, get_translation, get_global_translation and other method to keep axis in a var. (i need especially x and z axis)
I would like to adapt sprite’s direction to hte character’s direction but every test was wrong.
So i have in idea that is to compare the current position in x and the clik’s position ( the char moves with the mouse.
So i know that :
Cliks_position = get_viewport().get_mouse_position()
But Player_position = .???
And if player’s position is ok, writting this can be accepted?:
if Player_position.x > Cliks_position.x :
Or should i write other line to do this?
Thx

.

sorry for the underscore that doesn’t be displayed, i Don’t know why.

KKIILLOOUU | 2020-06-05 00:35

:bust_in_silhouette: Reply From: kidscancode

FYI, you need to indent code or wrap it in backticks like this for formatting, or use the “Code Sample” button in the editor when you’re writing your post.

3D nodes don’t have a position property. All 3D nodes inherit from Spatial:

A Spatial node has a transform (and a global_transform). Transforms contain the position, rotation, and scale information of the body in the form of a matrix. You can access the individual components by transform.origin - the coordinates in 3D space, and transform.basis - which contains the orientation.

More about transforms here:

The other problem I see in your code is that you can’t use the mouse position in 3D. The mouse doesn’t exist in 3D. It is “stuck” to the 2D plane of your camera. It doesn’t have any “depth” into the screen, so you can’t get its “position” relative to a 3D object.

To click-and-move in 3D, you need to project the mouse position “into” the world and see what it contacted. Collision objects can do this automatically, so if the ground is a StaticBody, for example, you can connect its input_event signal and it’ll detect when and where it’s been clicked on. You can use this coordinate to determine how to move your character.

Ok i will learn and try to understand how it works.

thx

KKIILLOOUU | 2020-06-05 09:54

So it works fine. Using the marker that you explain on your site for my animated sprite. But i have deleted the part wich allowed to rotate the kinematic body. Because my camera became crazy.
So for this moment my code is :

		$Marker.transform.origin = click_position
	if click_position.z < $Navigation/Unit.transform.origin.z:
		$Navigation/Unit/AnimatedSprite3D.play("Walk_N")
	        if $Marker.transform.origin == $Navigation/Unit.transform.origin:
	                 $Navigation/Unit/AnimatedSprite3D.play("Idle_N")

	if click_position.z > $Navigation/Unit.transform.origin.z:
		$Navigation/Unit/AnimatedSprite3D.play("Walk_S")

of course i will upgrade this for 8 direction( the sprite is in 2D)

You see that i tried to add Something like that after movement is finish :

		        if $Marker.transform.origin == $Navigation/Unit.transform.origin:
	                 $Navigation/Unit/AnimatedSprite3D.play("Idle_N")

But this part doesn’t work. Have any idea? This code is in spatial 3D (main scene)

Thx, because i see you take time to writte answer

EDIT : it’s done, i found a way to display sprite related to direction

KKIILLOOUU | 2020-06-05 17:36