Topic was automatically imported from the old Question2Answer platform.
Asked By
ThreeSpark
I want to make my player go in the opposite direction when it gets damaged, but I don’t know how to tell what direction the body is coming from - if the body is coming from the right, I want my player to go left, if the body is coming from the left, I want it to go right, if the body is coming from the top, I want it to go down, and if the body is coming from the bottom, I want it to go up. Any way I could do this?
Just compare the position of the enemy with that of your player. Assuming your player script checks for damage and will be calling the knockback function, you could do something like:
func knockback(enemy):
if enemy.position.x > position.x:
vel.x = -10
if enemy.position.x < position.x:
vel.x = 10
You can alter this for knockback along the y axis, as well as swap out vel for position or acceleration or whatever physics you are using.