Best body type to use for enemy

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JohnMaddenGodot

I am trying to figure out the best body type to use for an enemy in-game. It will be most similar to the Geemers from Metroid, in that they move in a linear direction across whatever surface they are on, and after making contact with a wall, rotate themselves accordingly depending on the position they are moving in and what surface they are on. I am currently using a RigidBody2D for doing so, however this may not be the right type and I am considering using a basic Area2D with a hitbox, but I still have not found a way to reliably determine what side contact was made with.

:bust_in_silhouette: Reply From: Vérszem

If your Player is a KinamticBody2D, than you shouldn’t use Rigidbody as an enemy. It will be buggy, when the Player touch it, and if the Player jumps to the top of it, it will glich to the floor. I think, you shold use a KinematicBody, as an enemy.

Docs say that KinematicBody is best used for the player. Would using that for an AI controlled enemy result in any other caveats that pop up later?

JohnMaddenGodot | 2022-05-24 14:32

A kinematic body enemy will not allow other kinematic bodies to move through it. Unless they are on different physics layers.

BigTuna675 | 2022-06-02 19:02

:bust_in_silhouette: Reply From: BigTuna675

If you want to have precise control over an enemy’s movements, you should either use kinematic body or area2d. I personally like to use Area2D for enemies, and then decide in code if I want the player to be knocked back or something else when they “collide”. If you don’t program any behaviour, then the player will be able to walk through the Area2D freely. This may or may not be what you want.

In your case specifically, it sounds like using either an Area2D or a Kinematicbody2D along with some Raycast2D nodes (to detect when the enemy is near a wall, or vertically, if it is nearing an edge) will get you the behaviour you are looking for.

In general though, this is how I understand the different physics bodies:

Rigidbodies are more suited for things that you don’t necessarily need precise control over, but still want to simulate and respond to physics. Imagine a beachball. It will bounce and roll and respond to gravity when it is kicked, but it won’t move other objects unless they are also rigidbodies. It does all of this for free, meaning you don’t need to program its behaviour. It acts according to the physics engine.

Kinematic bodies are physics objects, but they only react to physics events if you tell them to. For example, they will collide with other kinematic bodies and rigidbodies if they are on the same collision layer, but they wont bounce or reflect according to physics unless you tell them to specifically in code. They will not do anything that you don’t explicitly tell them to do (or if they are being aggressively bullied by another kinematicbody…)

Kinematic bodies can push around rigidbodies, but rigidbodies cannot push around kinematic bodies (unless you specifically code the kinematic body to respond to a contact with a rigidbody. I generally don’t like to handle it that way but it is possible AFAIK)

Area2D is not a physics body, so kinematic bodies and rigidbodies will not interact with them, unless specifically coded to do so. You can use the “on body entered” signal or “on area entered” signal and their variants to program behaviour between these.

You can still directly control a rigidbody using something called integrate forces but I think that is a little over the top for the enemy you’re describing.

Some more examples of this:

RB meets RB, they will interact completely according to physics, without being coded to do so. Two beachballs, they can push eachother around and interact with the physical environment “freely” without being explicitly told how to react.

KB meets RB, the KB will push the RB around.

KB meets KB, they will interrupt eachother’s movement. Generally, I think they both end up stopping eachother from moving if they are trying to move through eachother. Depends on exactly how they are programmed though. I have had enemy AI as kinematic bodies end up pushing the player kinematic body around if the player got completely swarmed/a lot of agents on top of it at once. It was like the player was frozen in place because it was unable to move these immovable objects around. But if it was just one KB object attempting to through another KB object, it should be the same as if both objects are trying to move through an immovable wall, and nothing moves.

Good luck!