- Home /
Dynamic Wander A.I.
For a project I need to make use of a number of AI techniques, and I'm stuck on dynamic wander. The purpose of this is to create a circle a certain distance in front of the player's orientation and then move to that point and repeat. My current code only seems to move the player in all directions instead of just mostly forward.
void wand(){
Vector3 objPos = new Vector3(transform.position.x + (transform.forward.x*10),0,transform.position.z + (transform.forward.z*10));
Vector3 source = (Random.insideUnitSphere * 10);
targetLocation = new Vector3(source.x+objPos.x,0, source.z + objPos.z);
}
transform.forward is the equivalent of new Vector3(0, 0, 1), so
(transform.forward.x*10) is the equivalent of 0 * 10.
So the vector objPos:
Vector3 objPos = new Vector3(transform.position.x + (transform.forward.x*10),0,transform.position.z + (transform.forward.z*10));
will have a constant x and y value, while the z value will increase by 10 after every execution.
But you are not moving your object in this code. Where is this method called?
@hatake3 You are incorrect here.
transform.forward provides the local forward direction of the transform. You are describing Vector3.forward
Steering behaviour is not meant to move the object (that belongs on the locomotion behaviour). Steering behaviours simply output a direction to travel in.
Answer by Kiwasi · Sep 21, 2014 at 02:45 AM
Your algorithm isn't correct. You should be using a point on the perimeter of a circle (for 2D movement as you described). You should not be picking a random point each frame, rather you should be moving a random distance around the perimeter of the circle.
The algorithm typically runs like this
Offset the target position from the current target position by a random amount
Project the target position back onto the circle centred on your vehicle
Project the circle in front of the vehicle.
Code to come when I get a moment.
Edit: Code as follows. Should work, untested.
Vector3 steeringDirection = new Vector3();
float circleOffset = 5;
float circleRadius = 2;
float maxJitter = 0.5f;
void Wander (){
Vector3 targetPosition = steeringDirection - transform.forward*offset;
targetPostion = new Vector3 (targetPosition.x + Random.Range(-maxJitter, maxJitter), 0, targetPosition.z + Random.Range(-maxJitter, maxJitter));
targetPosition.Normalise();
targetPosition = targetPosition * circleRadius;
steeringDirection = targetPosition + transform.forward*offset;
}
The output from this function goes into steeringDirection. You could easily pass it back as a return parameter instead.
You probably want to use invoke repeating or some such to provide frame rate independence. Otherwise this function will produce noticeably different behaviour based on the frequency of recalculation of the steeringDirection.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Keep flies in a sphere? 1 Answer
Dynamically Update Instantiated Objects Position 1 Answer
Projectile Terrain Destruction 1 Answer