if statment not working

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

i making that when you hit enemies from the side you lose health and bounce back in my platformer game

enemy calling the function

func _on_side_checker_body_entered(body):

hurt function

func hurt(var enemyposx):
velocity.y = -200

if position.x < enemyposx:
	velocity.x = 200
if position.x > enemyposx:
	velocity.x = -200
PlayerVars.health -= 0


What exact thing that doesen’t work? Did console print < or >?

USBashka | 2022-07-14 11:51

it did print >
no matter which side i hit the enemy

Ceo-Potato | 2022-07-14 12:02

print (“player”, position.x)

print(“enemy”, enemyposx)

ramazan | 2022-07-14 12:36

:bust_in_silhouette: Reply From: SanderVanhove

Could it be that the body in the first function is the child of another node? What could fix the problem, in that case, is using the global_position instead of the normal position.

Like this:

func _on_side_checker_body_entered(body):

It might be necessary to change the position in your hurt function to global_position too.

position is always relative to the parent of that node, while global_position is the position relative to the root node.

:bust_in_silhouette: Reply From: Gluon

Try this instead

if self.global_position.x < enemyposx:
    velocity.x = 200
if self.global_position.x > enemyposx:
    velocity.x = -200

Yeah. And hurt() i think sould be called with global_position.x too.

USBashka | 2022-07-14 13:38

didn’t work :frowning:

Ceo-Potato | 2022-07-14 14:35

is enemyposx also a capture of the enemies global_position.x or the enemies position.x?

Gluon | 2022-07-14 14:42

here is where i call the hurt func

func _on_side_checker_body_entered(body):

Ceo-Potato | 2022-07-14 17:28

Okay so I am assuming that is your enemy? Try changing it to this

func _on_side_checker_body_entered(body):

Gluon | 2022-07-14 17:37