- Home /
Random movment, like mobs in wow
I want to make my enemy character to get a random rotation, then move for about 3 sec, then stay get a new rotation and move again. And keep doing that until the player comes in range.
Here is my code:
function Idle() { while(true) { if(dist > awareDist) { randRotation(); yield WaitForSeconds(5); walkRandom(); yield WaitForSeconds(5); } } }
function randRotation() { transform.rotation = Quaternion.AngleAxis(Random.Range(-40,40), Vector3.up); }
function walkRandom() { var forward : Vector3 = transform.TransformDirection(Vector3.forward); var controller : CharacterController = GetComponent(CharacterController); controller.SimpleMove(forward * speed); }
The Idle function is called from Start. Animations will be added. dist is the distance to the palyer.
Answer by Justin Warner · Dec 05, 2010 at 02:31 AM
http://answers.unity3d.com/questions/30008/random-direction
Also, you can go off: http://answers.unity3d.com/questions/203/how-to-generate-a-random-number-inside-unity
Get a switch statement, and each case, have it do a certain action, maybe 1,3,6,8 all rotate accordingly; 10,30,90,130 degrees, then 2 and 7 move the object forward accordingly; 5 units, 10 units, and numbers 4 and 9 just pause for the 2 second yield in between...
Or you could make multiple functions, randomly chooses a function, and that function has a script that uses coroutines that make the character return to its original spot as if it doesn't it could fall off the cliff... lol
But yeah, 3 ways to go about it.
Search next time.
=)