Enemy Knockback

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

Hello!

I am making a top-down game with a Player that can shoot a bullet.

How do I implement a knockback on the bullet that pushes an Enemy AWAY from the Player’s position and not the bullet’s position?

:bust_in_silhouette: Reply From: Gluon

If you have a script in the top node connected to a signal then your enemy could send out this signal to ask for data. The top node can then check the players position, send out a further signal with an argument containing the players position, if your enemy has a boolean to determine if it has been hit or not this signal from the top node can connect to a function in the enemy to check if the boolean is true and then move away from the vector contained as an argument in the signal.

What do you mean by top node?

Is that the World node?

Torvic | 2023-02-26 11:26

In your scene you should have a tree of the nodes, one of these nodes will have the rest of the tree hanging off of it. This is the node I am referring too

Node
 ❘
 ❘_PlayerNode
 ❘_EnemyNode

this node can directly see the other nodes in the tree beneath and can therefore see both where the player and enemy are so it will be able to locate the players position and provide that as an argument in the signal.

Gluon | 2023-02-26 11:54

Can’t I just use get_tree().get_first_node_in_group(“player”) and then use player.global_position to get the player’s position?

BTW this is the project I am working on by Branno.

Torvic | 2023-02-26 15:49

Yes you can but i have done this in projects before and found it finicky. I ended up having a lot of code to deal with null return values in various situations whereas taking the top node in the tree I found this far more reliable. If you prefer to go down that route though you can certainly do so.

Gluon | 2023-02-26 16:01