system
July 14, 2022, 11:42am
1
Attention
Topic was automatically imported from the old Question2Answer platform.
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
system
July 15, 2022, 3:01pm
2
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.
system
July 14, 2022, 1:35pm
3
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
Ceo-Potato | 2022-07-14 14:35
is enemyposx also a capture of the enemies global_position.x or the enemies position.x?
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)