0 votes

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")
Godot version 3.4.4
in Engine by (21 points)
edited by

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

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

check
print ("player", position.x)

print("enemy", enemyposx)

2 Answers

0 votes

Try this instead

if self.global_position.x < enemyposx:
    print("<")
    velocity.x = 200
if self.global_position.x > enemyposx:
    print(">")
    velocity.x = -200
by (1,685 points)

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

didn't work :(

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)

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)
0 votes

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.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.