How to Prevent Two CharacterBody2D Nodes From Pushing Each Other

Godot Version

4.1.3

Question

Hello, I’m trying to make a game in which the player controls two different characters, one at a time. I’ve realized that sometimes the character I’m controlling will push the other one, especially if I’m trying to rotate the current character’s collision shape. I need to be able to rotate the collision shapes for this game without causing the other player to glitch and move to the side. I also want the two characters to be able to collide with each without pushing the other, so I can’t just change their collision layer.

Does anyone have suggestions about how to fix this issue?

You could give the players both a StaticBody2D in addition to their CollisionShape2D and alter their properties to have two different colliding boxes effectively. The collision shape can be toggled with the ‘disabled’ property and the static body can be toggled with the collision layer. The code would function like:

func _physics_process(delta):
    if currentPlayer:
        #enable CollisionShape2D
        #disable collision layers for StaticBody2D
    else:
        #enable collision layers for StaticBody2D
        #disable CollisionShape2D

Collision layer is ok. Then you check between two players body_entered and do something

func _on_Area2D_body_entered(body):
	if body.name == "Player":
		#do something

func _on_Area2D_body_exited(body):
	if body.name == "Player":
		#do something

Player 1:
image

Player 2:
image