enemy behavior with boundary to change direction
- by BadSniper
I'm doing space shooter kind of game, the logic is to reflect the enemy if it hits the boundary. With my logic, sometimes enemy behaves like flickering instead of changing the velocity. It's like trapped in the boundary and checking for if loops.
This is my code for velocity changing:
if(this->enemyPos.x>14)
{
this->enemyVel.x = -this->enemyVel.x;
}
if(this->enemyPos.x<-14)
{
this->enemyVel.x = -this->enemyVel.x;
}
How can I get around this? Its going out of boundary and don't know where to go and after sometimes its coming into field. I know whats the problem is, I dont know how to get around this problem.