[Solved] Make enemy fly towards player but not at diagonal

I am trying to get an enemy in a platformer to only fly up and down left and right towards the player - not diagonal. I have tried many things, but this seems like it should work:

If The Y Position of Enemy > Player.Y() then add to Enemy an instant force of 0 p/s on X axis and -80 p/s on Y axis

If The Y Position of Enemy < Player.Y() then add to Enemy an instant force of 0 p/s on X axis and 80 p/s on Y axis

If The Y Position of Enemy = Player.Y() then move Enemy toward Player with an instant force of 80 p/s

The problem is the Y position is never equal to the Player.Y()! The enemy will fly up and down and match the players Y position, but then it will just twitch up and down and never move left or right. What am I missing? How does it move through the equals to without ever reaching equal?

Update:
This doesn’t work either. I’m so confused: Why are they never equal?

If The Y Position of Enemy > Player.Y() and The Y Position of Enemy does not equal Player.Y() then change the Y position of Enemy subtract 1

If The Y Position of Enemy < Player.Y() and The Y Position of Enemy does not equal Player.Y() then change the Y position of Enemy add 1

If The Y Position of Enemy = Player.Y() then move Enemy toward Player with an instant force of 80 p/s

I’m curious, what is the enemy? The x,y values aren’t integers, the odds of the 2 being exactly the same is highly unlikely. Even if they both start as an integer, as soon as the player moves, it’s unlikely to stay at exactly #.0

You could use compare 2 numbers to compare the distance and allowing for a small variance.

(Just in case: abs() is absolute. It makes the value positive. If the difference between the object’s height is above or below zero, it’ll always return a positive number)

2 Likes

Keith_1357,
That totally worked the way I was hoping! I never would have guessed the x,y values weren’t integers. Nor would I have been able to figure out your solution. Thank you so much!

The enemy is a bat BTW. I know it makes more sense for the bat to fly around in any direction, but I really wanted this movement.

1 Like