Random Movement : 2d

Hello Everyone…

I am making 2D space shooter kind of game and in that i have scene with x-y plane.

Here i am translating my enemy object in y direction from top to bottom… But i want my object to change direction after crossing some random distance means i want my object to move left or right diagonally after some random distance…

But i am not getting the way to perform this behavior as i know how to translate object diagonally…

Code which i used is here :

        whatWayToGoInLife = Vector3.left * amttomove;
		whatWayToGoInLife = whatWayToGoInLife + Vector3.down * amttomove;
		transform.Translate(whatWayToGoInLife , Space.World);

But main problem is after crossing some distance on y axis i want my object to move in random direction…

Pleaze someone give me idea…

Thanks in advance for your support and help…

One way is to use Random.insideUnitCircle. I’m assuming your character is walking on the X/Z plane. Random.insideUniytCircle returns a Vector2, so you will need to covert it to a Vector3 to use as a direction. When you want to change directions, you redefine the movement vector:

Vector2 v2 = Random.insideUnitCircle;
direction = new Vector3(v2.x, 0.0f, v2.y);

And you code for movement might look like:

transform.Translate(direction * speed * Time.deltaTime, Space.World);