0 votes

Hi,
I have tried many way but i Don't understand how really getposition, gettranslation, getglobaltranslation 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 :
Cliksposition = getviewport().getmouseposition()
But Playerposition = .???
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

.

asked Jun 5 in Engine by KKIILLOOUU (32 points)

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

1 Answer

+1 vote
Best answer

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:
https://docs.godotengine.org/en/3.2/classes/class_spatial.html

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:
https://docs.godotengine.org/en/3.2/tutorials/math/matrices_and_transforms.html

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.

answered Jun 5 by kidscancode (16,966 points)
selected Jun 5 by KKIILLOOUU

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

thx

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.