I'm working on a chase game and I'm having a hard time getting my target sprite (the chased) to move away from the player sprite (chaser). Another problem I've been having is when I have the sprite cornered and I get close to it, it has a habit of shooting off to another corner. Truth be told I'm just having a hard time in general getting the behavior of the chased down... Ever attempt it gets faster as the chaser gets closer, while evil, would defeat the purpose of the game. Any help on how to make to make this work?
1. As distance gets less, make the chased move away. Hard to say why it's not working without knowing the details.
2. Maybe make the chased slow down the closer the chaser gets?
(see manual for "distance (x1,y1,x2,y2)")
Alright. So that helped. I now got the distance variable working so it reacts and accelerates when the Chaser gets close.
System 0<distance(Chaser.X, Chaser.Y, Chased.X, Chased.Y)<500 Set bullet Acceleration to 500
How would I make it choose a direction. My current action is
Set Bullet angle of motion to random(random(Sprite.8Direction.MovingAngle+random(180))
But all it does is cause the Chased to spiral around and freak out... which of course means it's not getting very far.
I'll add more to slow it down when the distance widens. I imagine adding a path finding behavior will allow it to move around obstacles.
Develop games in your browser. Powerful, performant & highly capable.
My first reaction would be to make it move directly away from the Chaser:
angle(Chaser.X, Chaser.Y, Chased.X, Chased.Y) + 180 (or -180, both lead to the same angle)
To get slightly more interesting/realistic behaviour, you might want to read up on Craig Reynolds' "Autonomous Steering Behaviours" - red3d.com/cwr/steer
For this example, what you're describing is the "Flee" behaviour, which applies a steering force (an acceleration, if you prefer) in the opposite direction from the direction to the player. However, I strongly encourage you to read the other behaviours too.