Adding an area 3D from code isn´t spawning at the position I want.

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

Hello all!

I am trying to create an area 3d that is a sphere and I want to get any bodys that are entered in, but I am having issues actually spawning the body and collision shape, I have printed the positions and they are the same as the block instance, since I can see the block but not the area somethings wrong with the area code, I enabled debug collision shape so I can see collisions and I dont see it any were, I would love for someone to see what mistake I have made, oh and also I cant seem to get the connect signal to work either because its not detecting anything or I did something wrong.

Code:

func create_area_sphere(pos : Vector3):
	# Create an area and collision shape for it
	var area = Area.new()
	var collision_shape = CollisionShape.new()
	
	# Create a shape for the collision
	var sphere = SphereShape.new()
	sphere.radius = 2.1
	sphere.margin = 0.04
	
	# Set the shape to the collision
	collision_shape.shape = sphere
	
	# Test to see if its actually working just instancing a block at the position
	var instance = block.instance()
	get_parent().add_child(instance)
	instance.translation = pos
	####
	#Add the are to the main scene
	get_parent().add_child(area)
	# Set the translation to the position
	area.translation = pos
	
	#Add the collision shape to the area as a child
	area.add_child(collision_shape)
	
	# Connect body entered signal to print_body
	area.connect("body_entered",self,"print_body")
	# Delete it when I am done with it
	area.queue_free()
	
	# Doesnt print anything
func print_body(body:Node):
	print(body)
	print("nothing")
:bust_in_silhouette: Reply From: Inces

You create a new area, add it to the scene, teleport it to desired position. Only now You assign a shape to it, and connect it after it actually picked up all collisions, to finally remove it.

Correct order should be :

  1. Create all parts of area
  2. Assign all parts to area ( add collision shape as a child )
  3. Connect the signal
  4. Add area as a child of a scene
  5. move the area to your location

Thank you very much, I been away from coding so I got a little rusty, thanks for your help!

Dragon20C | 2022-08-09 21:36