Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.normalise();
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.normalise();
desiredVel.scale( maxVelocity);

steering = desiredVel.sub(velocity);

steering = truncate(steering,maxForce);

steering.scale(1 / mass);

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

position.add(velocity);

Pattern

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
in the first image , the first line it says quarry is that a typo? –  Matthew Apr 28 '13 at 18:23
    
The quarry is the target. –  Byte56 Apr 28 '13 at 18:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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