Character Body 2D interactions

Godot Version

4.3

Question

Hello again,
I finished the tutorial by GDQuest and got everything working:

Both the player and enemies are using character body 2D. But for some reason, the player can push the enemies around, but the enemies cannot push the player. Obviously, this is the intended behavior, but I don’t understand why it’s working that way. Also is it recommended to have enemies use character body 2D?
Both the collision object 2D inspector values are basically identical, so the only difference seems to be in the code.
Player:

func _physics_process(delta: float) -> void:
	# Movement
	var direction := Input.get_vector("move_left","move_right", "move_up", "move_down")
	velocity = direction * 600
	move_and_slide()

Enemy:

func _physics_process(delta: float) -> void:
	var direction := global_position.direction_to(player.global_position)
	velocity = direction * 300
	move_and_slide()

Thank you for your time,
-Ruka

Edit: as always, I figure these things out after posting, sigh. It had to do with the collision masks. For some reason I thought the masks were just for detection signals, not also the things a character can actively interact with.

Still, if someone could elaborate whether it’s best practice to use character body 2d for NPCs, that would help a lot!

there are a few reasons to use characterbody2d for enemies rather than something else. all the different physics nodes have shares functions and properties, but they also have things that make them different.

area2D is super useful, but is only really meant to be for detecting when things are colliding or if another physics node has entered its zone, but it doesnt have move_and_slide() or any of the other things really useful for a character.

rigidbody2d is good because it shares some things with characterbody2d like move_and_collide() and get_gravity() which let it act more like a thing that exists in physics space, and it has lots of cool things of its own, but those things are more centered around things that feel like “objects” like a falling rock or a car or something. you can change things like its center of mas and apply forces to it, but not much useful for a character.

Characterbody2d is move_and_slide(), but it also has a while bunch of things that are really useful for making a character move in precise, animated ways, not to mention its the only node besides characterbody3d that has its velocity as a property you can use, as opposed to rigidbody2d where you dont have access to how fast youre moving and in what direction without some workarounds.

dont get me wrong, if you can find a good way to use a rigidbody2d for a character then go ahead! dont let convention get in the way of your creativity, but there is good reason people reccoment the characterbodies for characters.

2 Likes

Thank you for the detailed response! The reason I was asking is because the documentation for character body 2D seems to imply that it should only be used for player characters:

“CharacterBody2D is a specialized class for physics bodies that are meant to be user-controlled.”

So I was a little confused when the tutorial was using them for enemies. I come from unity, so it seems that this is pretty much just a kinematic body, and can be used for anything that doesn’t want forces, but still wants to lightly interact with the physics system. Perfect for my game :+1:

Also, I just wanted to add what an absolute joy prototyping in GDscript and Godot is. The ease with which I can set up singletons as managers, connect signals around through drag and drop directly into code where an export reference isn’t necessary, absolutely no boilerplate in GDscript, and by far the best feature of all, no massive laggy compilation on every code change is a breath of fresh air! I know it’s not quite as fast, but honestly that’s not a big deal at all.
Thanks,
-Ruka

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.