States transitioning affects physics

Godot Version

4.2.1 stable


Currently, I have an enemy in the scene where the player is. The enemy has the same code as the player except the player input function was commented out to take care of some errors. Everything is handled in states like, idle, move, fall, jump, etc. When the enemy is thrown with an attack feature I added, the enemy will have added velocity in the direction of the cursor, except the enemy will stop at the apex of the motion and it enters the fall state and falls straight down instead of completing the arc through the air. This physics interruption can be solved by removing the player input function from the enemy’s fall state. I guess my question is about how I would handle this because won’t this mean that this same result would be replicated in a PVP multiplayer scenario? If the player movement mechanics are handled through states then should I use that code for the enemy and add functions within those states for the enemy AI that detects certain things and thats how it will change states?

From what I understand, enemies and players have the same functions. So why not make this a bit more modular?

Your player and enemy can have the same script, but different “brains” that provide the same values by different means. So for a rough example;

if brain is player: 
move_me = brain.get player_input() 

if brain is enemy:
move_me = brain.get_enemy_movement()

velocity = move_me * speed

get_player_movement() would be an input.get_axis() for wasd, get_enemy_movement() would be whatever logic you use for your enemy AI. That way, you can reuse everything else. With the player, inputs control the variables the state uses, with the enemy, your AI would control the same variables.