I'm trying to make enemy follow player as in picture below, the code below is code for following player with steering behavior ( i'm not sure it steering works tho but it does follow player ), how would i code something like in the picture below to make the following more randomized but still going in direction of where player is.

Vector2f velocity,steering,desiredVel;
velocity = new Vector2f(player.getX() - getX(), player.getY() - getY());
velocity.scale( maxVelocity);

// unsure what to do with random numbers yet                
Random rand = new Random();
int randy = (rand.nextInt(101) -50);
int randx = (rand.nextInt(101)-50);

desiredVel = new Vector2f(player.getX() - getX() +randx, player.getY() - getY() +randy);
desiredVel.scale( maxVelocity);

steering = desiredVel.sub(velocity);

steering = truncate(steering,maxForce);

steering.scale(1 / mass);

velocity = truncate(velocity.add(steering) ,maxSpeed);
this.velocity = velocity;




This is a simple mixing of two different steering behaviors. Following and wandering.

enter image description here

enter image description here

You can find information about implementing both in this GDC paper from '99.

Start with implementing both independently. Then apply both at the same time, with a blending factor. You'll have to modify the blending value depending on how much you want to wander vs how direct you want the steering to be.

If you want the regular pauses, you can add a random duration for the path following to run and another random duration for it to pause.

  • \$\begingroup\$ in the first image , the first line it says quarry is that a typo? \$\endgroup\$
    – Matthew
    Apr 28 '13 at 18:23
  • \$\begingroup\$ The quarry is the target. \$\endgroup\$
    – House
    Apr 28 '13 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.