Collision shapes with different layers inside the same CharacterBody3D node

Godot Version

Godot v4.3 with C# support

Question

` Hi, have a great day everyone.
My name is Kléber, I started learning Godot a few months ago and I’m liking it a lot

During my learning path I faced a challenge for which I didn’t quite find a solution yet, even after a while searching in the web.

In my project, I have a CharacterBody3D object, which has a given capsule collision shape to interact with the environment (floor, walls, etc.). Currently, I’m wanting to add in the same body another collision shape, using a different shape and also interacting physically with other bodies (not the environment)

So basically, to sum up, I’m wanting to have a node structure like this:

CharacterBody3D
|-- CollisionShapeEnvironment
|-- CollisionShapeOtherBodies

and I want:

CollisionShapeEnvironment: capsule shape, layer mask 0
CollisionShapeOtherBodies: box shape, layer mask 1

so, two different collision shapes, which interact in different physics layers, but both affect the CharacterBody3D.

I saw some other similar discussions in the web, where people suggested to use the second collision shape inside an Area3D or even another CharacterBody3D. I tried some things in this alike, but didn’t have any luck so far.

Does anyone knows how to tackle this type of challenge?

Thanks a lot everyone.

EDIT:

Just to elaborate a little bit more on my intent, I want my character to collide with other characters in the same way it collides with the floor and walls, but using a different collision shape instead. That’s why I was trying to have one shape with a layer mask of the environment, and another one with the layer mask of the characters.
`

Collision layers and masks are on the CollisionObject3D, which is the CharacterBody3D. You can’t have a per-shape collision layer / mask set. You’ll need two use two CollisionObject3Ds if you want to have two sets of layers / masks at once.

Can you say what things you tried that didn’t work?

Hey, thanks for your reply @paintsimmon .

I tried two node setups, one of them was:

CharacterBody3D
|-- CollisionShapeEnvironment
|-- Area3D
__|-- CollisionShapeOtherBodies

and the other one was the same node organization, but using a CharacterBody3D instead of a Area3D. With both approaches I didn’t see any influence of the inner Area3D/CharacterBody3D in the root CharacterBody3D.

I don’t know what the influence means. What do you want to happen?

I put a edit section earlier in order to try to clarify better. Does this help to clarify the intent? I can elaborate further if needed.

Thanks in advance for the help.

I don’t think this is going to be the best solution, but the closest I could get it.

extends CharacterBody2D
@onready var col_env: CollisionShape2D = $CollisionShapeEnvironment
@onready var col_bod: CollisionShape2D = $CollisionShapeOtherBodies
@export var speed = 500


func _physics_process(delta: float) -> void:
	var move = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	var position_before_bodies = global_position
	velocity = move * speed
	if !move_and_slide_bodies():
		global_position = position_before_bodies
		velocity = move * speed
		move_and_slide_enviroment()


func move_and_slide_enviroment(test = false) -> bool:
	print("env")
	col_bod.disabled = true
	col_env.disabled = false
	set_collision_layer_value(1, true)
	set_collision_mask_value(1, true)
	set_collision_layer_value(2, false)
	set_collision_mask_value(2, false)
	return move_and_slide()


func move_and_slide_bodies(test = false) -> bool:
	print("bodies")
	col_bod.disabled = false
	col_env.disabled = true
	set_collision_layer_value(1, false)
	set_collision_mask_value(1, false)
	set_collision_layer_value(2, true)
	set_collision_mask_value(2, true)
	return move_and_slide()

Example

There’re issues with this when colliding with the bodies and environment at the same time letting you to go through environment or body during a collision.

Edit: Sorry missed this was C#

Heey @KurtBlissZ, thanks a lot for the suggestion, I’ll give it a try later!