+1 vote

I'm working on a game with a handful of CanvasLayers (for custom parallax purposes). I'd like the player to be able to interact with certain objects, but it seems that by default collisions aren't reported between layers.

If I have an Area2D1 on CanvasLayer1 and an Area2D2 on CanvasLayer2, I can connect the "area_enter" signal for both of them but neither will report on each other, even though they will report on other Area2Ds within their own CanvasLayer.

Is there any way to monitor collisions across CanvasLayers?

Edit: It looks like separate CanvasLayers have separate World2Ds. I'm willing to delve into Physics2DDirectSpaceState related answers if need be.

in Engine by (103 points)
edited by

2 Answers

+2 votes
Best answer

I did this with a RigidBody in one CanvasLayer, and then a StaticBody inside another CanvasLayer. The code here is written inside that StaticBody that I used, and it acts as a simple floor.

What you do is use the Physics2DServer to assign a new space to the body. You pull the RID from the body, and the RID from the World2D of the desired CanvasLayer. Though I don't know what other considerations or side effects this might have in your design.

extends StaticBody2D

func _ready():
    var target_layer = get_parent().get_parent().get_node("CanvasLayer")
    Physics2DServer.body_set_space(get_rid(), target_layer.get_world_2d().get_space())
by (5,202 points)
selected by

Brilliant, thanks! I had to use Physics2DServer.area_set_space, but the idea's the same.

Much better than my interim solution of having the Area2Ds reparent themselves to the gameplay layer then follow their old parent manually.

You're welcome, glad it helped.

hi avencherus, i have a similar question (or not) could you have a look at it please?

The link : https://godotengine.org/qa/12082/setting-parallaxlayer-collision

Thank you in advance.

0 votes

hi avencherus, i have a similar question (or not) could you have a look at it please?

The link : https://godotengine.org/qa/12082/setting-parallaxlayer-collision

Thank you in advance.

commented 1 hour ago by mokalux

by (167 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 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 webmaster@godotengine.org with your username.