Axolotl_10110038
Axolotl_10110038

Reputation: 27

Collision detection with Godot 4.0 3D

I am a newbe to programing in GDScript 2.0 and am new to GD in general. I need to set the player position to 0,0,0 when touching an object that is in the group called "lava".

I am using godot 4.0 and trying to make a 3d game.

Code:

func _body_entered(body):
    if is_in_group("lava"):
        character_body.translation = Vector3(1, 1, 1)
        # Set the position of the node to (x, y, z)`[enter image description here][1]

It is not giving me an error but it is not working.

Upvotes: 2

Views: 11277

Answers (2)

Theraot
Theraot

Reputation: 40315

Making a method called _body_entered is not sufficient. Make sure you connected the "body_entered" signal to your method.

You can do this form the editor, by selecting the Area3D or RidigBody3D you are using, going to the "Node" panel (by default docked on the right), and then double clicking the signal. Godot will ask you to which Node and which method to connect it to.

See connecting-a-signal-in-the-editor.


Addendum:

If the object is not an Area3D or a RigidBody3D (e.g. the object is a StaticBody3D) then it does not have a "body_entered" that you can use.

In the case of a StaticBody3D that does not move※, then detecting the collision in the CharacterBody3D (as OP answer suggests) is sufficient, because the collision would be a result of the motion of the CharacterBody3D and not the other way around.

※: The recommended way to make a moving platform in Godot 4 is with an AnimatableBody3D which are StaticBody3D.

Otherwise, I would suggest to change the StaticBody3D to an Area3D. I have outlined some alternative approaches to get "body_entered" in an StaticBody3D elsewhere.

Upvotes: 0

Axolotl_10110038
Axolotl_10110038

Reputation: 27

just do this

for index in range(get_slide_collision_count()):
    var collision = get_slide_collision(index)
    if collision.get_collider().is_in_group("Lava"):
        print("colided")

Upvotes: -1

Related Questions