• Godot Help
  • How to get body's position entered in Area3D ?

Hello,
Newbie here, i (try to) use Godot 3.5.2 in 3D mode with GDscript.

I have an Area3d (which is a lamp in my game) able to detect objects entering inside of it using the signal body_entered(body: Node)

so i have this basic script :

extends Spatial

func _on_Lamp_body_entered(body):
	print("[LAMP] -> coll enter", body)

It works. But what i want is to access to the position of the body inside the area... pretty much like in this pseudo code :

extends Spatial

func _on_Lamp_body_entered(body):
	var position = Vector3.ZERO
	#position = get body position
	print("[LAMP] -> coll enter", body," at ", position)

...And i just can't wrap my head around that. So how to access the position of the body ?

Thank's!

Which position do you want to know? That one marked X in this illustration? Or another one?

+-----------------+  lamp
|                 |
|                 |
|                 |
|   pos? --> X-------+  body
|            |       |
+------------|       |
             |       |
             +-------+

You can calculate the relative coordinates by using their global positions (body.global_position and the global_position of the lamp area).

    Toxe
    Hi, first thank you for your help!
    In fact... Which is the most suitable for my application ? X is fine yes or the body's origin maybe ? My goal is to draw a raycast to this position. This way i will know the distance between them.

    body.global_position and the global_position of the lamp area.

    An other problem here is when i type in "body." inside godot editor, it give not any completion, so i just can't find my way...

    Edit : For the test the body is a CSGBox

    • Toxe replied to this.

      Hiigaran An other problem here is when i type in "body." inside godot editor, it give not any completion, so i just can't find my way...

      Try defining the function as:

      func _on_Lamp_body_entered(body: Node3D):
          print(body.global_position)
          # ...

        Toxe I get this error :

        the identifier "Node3D" isn't a valid type (not a script or class), or couldn't be found on base "self".

        • Toxe replied to this.

          Hiigaran Oh, right, it's probably called something else in version 3.5. Sorry.

          Edit: I think it's Spatial. It would be Node3D in Godot 4.

          Yes it's spatial,

          func _on_Lamp_body_entered(body: Spatial):
          print(body.transform.origin)

          That's working now. Thank you!