- Home /
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.
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.
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.
Answer by fafase · Apr 10, 2012 at 08:17 AM
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); }
Thank you so much, I see now what a mess my script was) This one works perfectly!
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...
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.
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...
"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?
Answer by fafase · Apr 11, 2012 at 05:42 PM
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.
Your answer
Follow this Question
Related Questions
How to create random movement in 2d 2 Answers
Random Movement : 2d 1 Answer
How to create random movement in 2D 2 Answers
Unity 2D Random Movement 1 Answer
Need player to walk in random direction 2 Answers