x


Random movement direction cycle.

I wanted to create a script, that moves an object in one direction and changes the direction of the movement every three seconds. But it returns error. Could you please see what is wrong here?

    var speed = Vector3 (0, 0, 0);


function Start() 
{
var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
StartCoroutine(ChangeDirection(3.0));
}




function Update()
{
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);



}




function ChangeDirection (waitTime : float) 
{
yield WaitForSeconds (waitTime);
var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
StartCoroutine(WaitAndStart(1.0));
}

function WaitAndStart (waitTime : float) 
{
yield WaitForSeconds (waitTime);
var randomDirection : Vector3 = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
StartCoroutine(ChangeDirection(3.0));
}

Error is:

1)Assets/Blind_Scripts/Random_2D_Movement.js(35,16): BCE0070: Definition of 'Random_2D_Movement.WaitAndStart' depends on 'Random_2D_Movement.ChangeDirection' whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.

2)Assets/Blind_Scripts/Random_2D_Movement.js(35,15): BCE0023: No appropriate version of 'UnityEngine.MonoBehaviour.StartCoroutine' for the argument list '(unknown)' was found.

more ▼

asked Apr 10 '12 at 07:58 AM

Makiavel gravatar image

Makiavel
13 5 8 10

When you post, highlight the code part and press 101010 to make it easier to read. Also, you should post the error so that it can be fixed faster if we already know where it is wrong.

Apr 10 '12 at 08:02 AM fafase

Your problem is that one function relies on the other while the other has not yet given any result. the second error is telling you that you call a function that does not exist.

Apr 10 '12 at 09:50 AM fafase
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Ok, I reviewed your script just by head so try and report any error so that we can fix it.

    var speed : Vector3; 
    var timing :float;

    function Start() { 
       // I reckon you are in 2D so z is 0
       speed = Vector3(Random.Range(-359, 359),Random.Range(-359, 359),0); 
       timing = 0;
    }

    function FixedUpdate() { 
       timing+=Time.deltaTime;
       rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime); 
        if(timing > 3){
           ChangeDirection();
           timing = 0;
        }
    }

    function ChangeDirection () { 
        speed = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359),0);  }
more ▼

answered Apr 10 '12 at 08:17 AM

fafase gravatar image

fafase
11.1k 11 15 45

Thank you so much, I see now what a mess my script was) This one works perfectly!

Apr 10 '12 at 10:48 AM Makiavel

I'm sorry, but I've got another question. Is there a way to point an object of script in the direction of movement? I can not find a way to use a current Vector3 as a looking direction, not via LookAt, not via Rotate functions...

Apr 11 '12 at 11:23 AM Makiavel

Do you mean you can figure out LookAt()?

target:Transform; // drag your target object in the inspector

function Update(){ transform.LookAt(target); }

Or explain a little more I did not quite get what you meant.

Apr 11 '12 at 01:17 PM fafase

I mean that I am trying to make the object, that I attach this script to, face in the direction of the movement. And each time the direction of movement changes, so does the facing direction. And so on...

Apr 11 '12 at 02:49 PM Makiavel

"I wanted to create a script, that moves an object in one direction and changes the direction of the movement every three seconds." I am lost, that is what you wanted but now you don'ty want it?

Apr 11 '12 at 03:32 PM fafase
(comments are locked)
10|3000 characters needed characters left
var moveSpeed:float = 10;
var timing :float;
var target:Vector3;

function Start() { 
   timing = 0;
}

function FixedUpdate() { 
   timing+=Time.deltaTime;
   transform.position += transform.forward*moveSpeed*Time.deltaTime
    if(timing > 3){
       ChangeDirection();
       timing = 0;
    }
}

function ChangeDirection () { 
    target.position = Vector3(Random.Range(-359, 359),Random.Range(-359, 359),0);  }

Well , I tried to review with the information I understood, now you get a random position on the map to where the guy is heading. It changes every 3 seconds.

more ▼

answered Apr 11 '12 at 05:42 PM

fafase gravatar image

fafase
11.1k 11 15 45

(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:

x1426
x1071
x599
x296
x38

asked: Apr 10 '12 at 07:58 AM

Seen: 1953 times

Last Updated: Apr 16 '12 at 06:47 AM