x


AI wandering script?

I am trying to make a script to make an object wander around. So far I have this:

var speed : int= 5;
var wayPoint : Vector2;

function Start()
{
    InvokeRepeating("Wander", 2, 20);
}

function Wander()
{
    var wayPoint : Vector2= Random.insideUnitCircle *47;
    Debug.Log(wayPoint);
}

function Update() 
{
    transform.LookAt(wayPoint);
    rigidbody.AddRelativeForce(Vector3.forward * speed);
}

This makes the object move to the position I want but my problem is that since it's a vector2 it operates between the x and y axis. I want my object to move 2 dimensionally (between the x and z axis). When I leave the var wayPoint undefined when the game first starts the entire script doesn't work. Why is this? additionally I tried to make a while statement and then replaced it with an if statement saying that while(or if) the object was not dead that it would "Wander". Any help is appreciated. Should the function Update() be a Fixed Update instead?

more ▼

asked Jul 22 '10 at 12:54 AM

Panamamigo gravatar image

Panamamigo
126 13 13 23

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

FixedUpdate should be reserved for physics related functions.

You need to make wayPoint a global variable by declaring it outside of the function, put it at the top of your script, leaving it undefined obviously means that variable has no meaning and wont compile if you tried to use it in a function later e.g.

var wayPoint : Vector3;

function Start(){
   Wander();
}

You can also use Radom.insideUnitSphere instead and then set the y variable to whatever height your floor is e.g.

function Wander(){
   wayPoint = Random.insideUnitSphere*47;
   wayPoint.y = 0;
}

Instead of using invoke you could simply call Wander after you've reached the waypoint and the character is still alive. e.g.

var isAlive : boolean = true;

function Update(){
   if((transform.position - wayPoint).magnitude < 0.5 && isAlive){
      Wander();
   }
}

EDIT:

Here's your working code...

var Speed= 10;
var wayPoint : Vector3;

function Start(){
   //initialise the target way point
   Wander();
}

function Update() 
{
   // this is called every frame
   // do move code here
   transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    if((transform.position - wayPoint).magnitude < 3)
    {
        // when the distance between us and the target is less than 3
        // create a new way point target
        Wander();

        // don't need this 

        //transform.LookAt(wayPoint);
        //transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    }
}

function Wander()
{ 
   // does nothing except pick a new destination to go to
    wayPoint= Random.insideUnitSphere *47;
    wayPoint.y = 1;
   // don't need to change direction every frame seeing as you walk in a straight line only
    transform.LookAt(wayPoint);
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}
more ▼

answered Jul 22 '10 at 01:15 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Thank you spinaljack, I am still having some difficulties so I responded in the form of an answer so I could post my new code.

Jul 22 '10 at 01:49 AM Panamamigo

Thanks a ton man, it is working great!

Jul 22 '10 at 03:33 PM Panamamigo
(comments are locked)
10|3000 characters needed characters left

Here's an update to this, I was noticing it always defaulted to 0,0,0 and then picked waypoints from there. This will cause it to wander aimlessly from it's origin.

var Speed= 20;
var wayPoint : Vector3;
var Range= 10;

function Start(){
   //initialise the target way point
   Wander();
}

function Update() 
{
   // this is called every frame
   // do move code here
   transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    if((transform.position - wayPoint).magnitude < 3)
    {
        // when the distance between us and the target is less than 3
        // create a new way point target
        Wander();


    }
}

function Wander()
{ 
   // does nothing except pick a new destination to go to

    wayPoint=  Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range));
    wayPoint.y = 1;
   // don't need to change direction every frame seeing as you walk in a straight line only
    transform.LookAt(wayPoint);
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}
more ▼

answered Dec 21 '12 at 09:46 PM

Alpine Flame gravatar image

Alpine Flame
1

(comments are locked)
10|3000 characters needed characters left
var Speed= 10;
var wayPoint : Vector3;

function Wander()
{
    wayPoint= Random.insideUnitSphere *47;
    wayPoint.y = 1;
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}

function Update() 
{
    if((transform.position - wayPoint).magnitude < 3)
    {
        Wander();
        transform.LookAt(wayPoint);
        transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    }
}
more ▼

answered Jul 22 '10 at 01:48 AM

Panamamigo gravatar image

Panamamigo
126 13 13 23

don't post an answer... edit your question

Jul 22 '10 at 01:54 AM spinaljack

The reason it's moving slowly is because you're using force and the objects is either very heavy or has high friction. Change it to transform.position += transform.TransformDirection(Vector3.forward)*speed*Time.deltaTime

Jul 22 '10 at 01:56 AM spinaljack

also you've put your actual movement code inside a function and not the update, it'll only move one every time the function is called instead of every frame like it should

Jul 22 '10 at 02:02 AM spinaljack

I tried that, but it still isn't working with the transform.position += transform.TransformDirection...etc. When I use that it does not move at all. When I use other methods like transform.Translate or something it only moves a fraction of the way. What could be wrong? Thank you so much for all your help spinaljack it's really helping me out! I edited my question with the code.

Jul 22 '10 at 06:00 AM Panamamigo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3739
x960
x12

asked: Jul 22 '10 at 12:54 AM

Seen: 4819 times

Last Updated: Dec 21 '12 at 09:46 PM