- Home /
How to create random movement in 2D
My goal is to have human characters run in a randomly chosen direction (up, down, left, right) and keep going in that direction until they run into something (then they would change direction). Here is what I have for that right now:
//Decides which direction the humans will run
function Update() {
var randDir = Random.Range(1,4);
//Right
if (randDir == 4) {
person.transform.Translate(-Vector2.right*Hspeed*Time.deltaTime);
}
//Left
if (randDir == 3) {
person.transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
}
//Up
if (randDir == 2) {
person.transform.Translate(Vector2.up*Hspeed*Time.deltaTime);
}
//Down
if (randDir == 1) {
person.transform.Translate(-Vector2.up*Hspeed*Time.deltaTime);
}
}
This makes the humans basically just shake around. I understand why it does this, but does anybody know what I could do to get going in the right direction?
Answer by AlucardJay · Nov 27, 2012 at 03:50 PM
Pick a direction and keep applying it until it hits a wall. Then when that object collides with a wall, pick another new direction then apply that. Currently you are picking a random direction every Update.
The OnCollisionEnter function first checks for the name of the object you hit. For my example I have used a name check, and the arguement is looking for the name Wall. You also in future may run into problems where the object chooses a direction that is the same as the wall it just hit, and so may never leave the wall. Just be aware of this consideration =]
var randDir : int = 1;
function Update()
{
// movement here
// ....
switch( randDir )
{
case 1 :
person.transform.Translate(-Vector2.up*Hspeed*Time.deltaTime);
break;
case 2 :
person.transform.Translate(Vector2.up*Hspeed*Time.deltaTime);
break;
case 3 :
person.transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
break;
case 4 :
person.transform.Translate(-Vector2.right*Hspeed*Time.deltaTime);
break;
}
}
function OnCollisionEnter( other : Collision )
{
// what did the object just hit?
Debug.Log( "collided with " + other.collider.gameObject.name );
if ( other.collider.gameObject.name == "Wall" ) // for example wall is called Wall
{
randDir = Random.Range(1,5);
}
}
Thanks to Wolfram for pointing out my not understanding the question!
also note that Random.Range for integers, the highest value is exclusive (not included). This means to return 1,2,3 or 4, you need to use
randDir = Random.Range(1,5); Debug.Log( "randDir = " + randDir );
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Returns a random integer number between min [inclusive] and max [exclusive]
original answer :
e.g.
var timer : float = 0.0;
var timerMax : float = 1.5;
var randDir : int = 1;
function Update()
{
if ( timer > timerMax )
{
// pick a new direction
randDir = Random.Range(1,5);
// make a new random Max time
timerMax = Random.Range( 0.5, 2.5 );
}
// movement here
// ....
switch( randDir )
{
case 1 :
person.transform.Translate(-Vector2.up*Hspeed*Time.deltaTime);
break;
case 2 :
person.transform.Translate(Vector2.up*Hspeed*Time.deltaTime);
break;
case 3 :
person.transform.Translate(Vector2.right*Hspeed*Time.deltaTime);
break;
case 4 :
person.transform.Translate(-Vector2.right*Hspeed*Time.deltaTime);
break;
}
}
Answer by Wolfram · Nov 27, 2012 at 04:06 PM
In your script you choose a new random direction every frame. Instead, what you want is to make randDir global, and only change its value if your character actually collides with an object, using one of the OnCollision...()/OnTrigger...() functions.
Also note your bug concerning Random.Range(), which @Jay Kay explained.
Yeah, sorry. I just re-read the question and don't know where I got change direction after random time from?! Thanks for helping me see my misunderstanding, I shall fix the answer .... Answer is fixed, and upvote to you =]