# Knockback direction in finite state machine

i’m creating a game and having trouble figuring out how to get the character to get knock-backed in the correct direction
I’d like to change the knockback direction to this:

``````if ((character.global_position - who_applied_the_hit.global_position).normalized() > 0 ):
``````

but can’t figure out the who_applied_the_hit part of the code. The who_applied_the_hit is just a placeholder for the actual code. i’d like to code some function or logic that could recognize which enemy is hitting the player? but I have no idea how to do that

Any and all help is appreciated

Here’s the entire code for the knockback state

``````func knockback(force: float, x_pos: float, up_force: float):
if ((character.global_position.x - who_applied_the_hit.global_position.x) > 0 ):
character.velocity = Vector2(force * 2 * x_pos, -force * up_force)
else:
character.velocity = Vector2(-force * 2 * x_pos, -force * up_force)
``````

Looks like you might just be missing the .x in the if for your character position.

``````if ((character.global_position.x - who_applied_the_hit.global_position.x) > 0 ):
^^
``````

oh yeah lol thanks! but thats not the real issue I’m having. The who_applied_the_hit is just a placeholder for the actual code. Do you know how to code some function or logic that could recognize which enemy is hitting the player?

From where do you call the `knockback` function? How do you know when to call it? Is it possible to send along the `who_applied_the_hit` (or their position) as a parameter to the function?

i’m using a state machine and calling the knockback function whenever the players health is lessened. I’m not sure if its possible to send along the `who_applied_the_hit` (or their position) as a parameter to the function, but how would you code it?

Please ask if you need to see more of my code

I mean, seeing more of your code might be useful. I’m basically looking for: what’s a point in the code where you actually have a refence to the entity that applies the hit? Is it just before applying the damage? Earlier than that? And then can we use the reference from there?

I’m a beginner to using godot so a lot of my code is unorganised and not optimal. but

the enemy hurting the player logic (inside the enemy script):
(Game is a global script)

``````func _on_player_collision_body_entered(body):
if body.is_in_group("Player_body"):
Game.playerHP -= 3

``````

I have a group for all enemies:

``````func _on_ready():
animation.play("Idle")
add_to_group("enemy")
``````

And I wrote this but don’t know how to call enemy position ( ):

``````#when the enemy enters the area get its position? maybe?
func _on_knockback_collision_body_entered(body):
if (body.is_in_group(enemy)):
#get enemy name or position somehow whichever works
``````

Here is my node setup for my world (when the player is in the game)

Don’t know if this is enough

I really appreciate the help

Ah ok, so hitpoints are in a global script… You could do:

``````func _on_player_collision_body_entered(body):
if body.is_in_group("Player_body"):
Game.playerHP -= 3
body.knockback(some_force, some_x_pos, some_up_force, global_position)
``````

(here, I don’t know how you calculate force, x_pos, and up_force for the knockback function, so I just put some placeholder names - the point is that you can pass the position along there. The knockback function would need to be changed to take the extra parameter as well).

Alternatively, here’s how to get the enemy position in the last code snippet you posted:

``````#when the enemy enters the area get its position? maybe?
func _on_knockback_collision_body_entered(body):
if (body.is_in_group(enemy)):
var enemy_position = body.global_position
``````

I’ve used `global_position` rather than `position` in both cases, so it’ll still work if the `Mobs` and `Player` Node2Ds are moved.

1 Like

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