0 votes

Im working on a game with two playable characters at once. To complete each level I want for both characters to be present at the same time at the end of the level. For this Im using an area2D which uses getoverlappingbodies() and goes through that array to check if its colliding with both player characters (both of which are kinematic bodies)
It works when I check for only one of the characters but I cannot figure out how to require both characters colliding with the Area2D at once to change scenes.
Is this way wrong or is this there an easier way to go about this maybe?

Godot version 3.2.3
in Engine by (15 points)

2 Answers

+1 vote
Best answer

use the area2D body_entered signal, when both players are inside the area set two vars to true when both are true change scene

var player_1_inside_area = false
var player_2_inside_area = false

func _physics_process(delta):
 if player_1_inside_area and player_2_inside_area:
   change_scene()

func _on_Area2D_body_entered(body):
  if body.is_in_group("player_1"):
     player_1_inside_area = true
  elif body.is_in_group("player_2"):
     player_2_inside_area = true

and i think you could also use setget to check for the vars value change instead of checking them on process every frame.

by (454 points)
edited by
+1 vote

If the Area2D can only collide with your players simply check get_overlapping_bodies().size() > 1 inside of _physics_process() or _process(). Otherwise do something like this:

player_one_in_area = false
player_two_in_area = false
for body in get_overlapping_bodies():
    if body [some check that guarantees player one]:
        player_one_in_area = true
    elif body [some check that guarantees player two]:
        player_two_in_area = true

if player_one_in_area and player_two_in_area:
    #change scenes here
by (3,880 points)
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.