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):
body.hurt(position.x)

hurt function

func hurt(var enemyposx):
set_modulate(Color(.9,.5,.3,.7))
velocity.y = -200

if position.x < enemyposx:
	print("<")
	velocity.x = 200
if position.x > enemyposx:
	print(">")
	velocity.x = -200
	
PlayerVars.health -= 0

Input.action_release("right")
Input.action_release("left")

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

check
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):
    body.hurt(global_position.x)

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:
    print("<")
    velocity.x = 200
if self.global_position.x > enemyposx:
    print(">")
    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):
    body.hurt(position.x)

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):
    body.hurt(global_position.x)

Gluon | 2022-07-14 17:37