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
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?
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?
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)
(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.