Get world position of clicked node with signal callback?

mperonenmperonen Posts: 5Member
edited November 2018 in 3D

I have the following script in my container node which has two child StaticBody nodes:

How can I get the world position of that clicked node?

func _ready():
    for i in get_children():
        if i is StaticBody:
            i.connect("input_event", self, "OnClick", [i])



func OnClick(camera, event, pos, normal, index, node):
    if event is InputEventMouse:
        if event is InputEventMouseButton:
            if event.is_action_pressed("mouse click"):
                print(node) # I want this node's world position. Is it possible

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,043Admin
    Answer ✓

    Is the node 3D, or 2D?

    For 3D nodes, you can get the world/global position of a node like this:

    var world_pos = node.global_transform.origin;
    print (world_pos)
    

    For 2D nodes, you can get the world/global position of a node like this:

    var world_pos = node.global_position;
    print (world_pos)
    

    Hopefully this helps :smile:

  • mperonenmperonen Posts: 5Member

    Thank you very> @TwistedTwigleg said:

    Is the node 3D, or 2D?

    For 3D nodes, you can get the world/global position of a node like this:

    var world_pos = node.global_transform.origin;
    print (world_pos)

    For 2D nodes, you can get the world/global position of a node like this:

    var world_pos = node.global_position;
    print (world_pos)

    Hopefully this helps :smile:

    Thank you very much!

Sign In or Register to comment.