0 votes

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")
Godot version 3.4.4
in Engine by (164 points)

1 Answer

+1 vote
Best answer

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

by (7,742 points)
selected by

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

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.