+1 vote

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?

asked Aug 2, 2018 in Engine by ThreeSpark (88 points)

1 Answer

0 votes

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.

answered Aug 2, 2018 by Diet Estus (1,504 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 How to use this Q&A? before posting your first questions.