x


Simple Enemy Script Questions About Random.Range.

I have a pretty simple script to control an enemy. In the "Rest" function, a looped animation plays, where the enemy bounces up and down. I tried changing the animation wrap to Once, and adding a yield WaitForSeconds (Random.Range). What I wanted to happen is have the enemy bounce randomly, for instance jump, wait 2 seconds, then again, wait one, again, 3 seconds etc. All I get is a random number is chosen from the Range, and this is repeated every time, for instance a constant 2 second interval. Is there any way to achieve what I'm after please?

Also, when I make multiple enemies, they all act identically (obviously). Is there any way of using the same script on, say three of them, where the random time between animations is different? I'm after the three side by side, bouncing randomly, so they're not in perfect unison.

Any help gratefully received, thanks!

more ▼

asked Jan 21 '12 at 04:35 PM

husbandofemily gravatar image

husbandofemily
94 6 9 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

Not sure how any of your script is set up, but maybe you can piece some of this together with it.

var randomJumper : float ;
var jumping : boolean = false ;
var rangeMin : float = 1 ;
var rangeMax : float = 5 ;
 
function Update(){
   if(!jumping)
      Jump_N_Stuff() ;
}
 
function Jump_N_Stuff(){
   randomJumper = Random.Range(rangeMin,rangeMax) ;
   jumping = true ;
 //do some jump animation clip thing
   yield WaitForSeconds(randomJumper) ;
   jumping = false ;
}
more ▼

answered Jan 22 '12 at 02:05 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 9 39

(comments are locked)
10|3000 characters needed characters left

And the relevant part of the new, working one..... Thanks again :)

function Update () 

    {

    var atp = GameObject.Find("HammyActor");

    var avoidThis = atp.transform.position;

    if (alive){



       var dist = Vector3.Distance(avoidThis, transform.position);

         if (dist < safeZone)

        Chase();

           else if (!jumping)

           Rest();

        }

    }



function Chase()

{

animation["ArmatureAction"].wrapMode = WrapMode.Loop;

var target2 = GameObject.Find("HammyActor");

var target = target2.transform.position;

animation.CrossFade("ArmatureAction");

transform.LookAt(target);

transform.Translate(0,0,speed);

}



function Rest()

{

var timerandom = Random.Range(1,9);

animation["ArmatureAction"].wrapMode = WrapMode.Once;

jumping = true;

    animation.CrossFade("ArmatureAction");

    yield WaitForSeconds(timerandom);

    jumping = false;

}
more ▼

answered Jan 22 '12 at 02:51 PM

husbandofemily gravatar image

husbandofemily
94 6 9 12

(comments are locked)
10|3000 characters needed characters left

Lo0NuhtiK - Thanks for your help, tweaked my code to mirror yours (it was quite similar anyway), and it worked a treat! I just need to find out why now, for my own benefit! Here was my code:

 var speed = 0.3;
    var speed2 = 0.5;
    var safeZone = 10;
    var turnSpeed = 3;
    var alive = true;

    function start(){

        animation["ArmatureAction"].wrapMode = WrapMode.Once;
        animation["die2"].wrapMode = WrapMode.Once;
        animation["die2"].layer = 1;
        animation["ArmatureAction"].layer = 0;
        animation.Stop();
    }
    function Update () 
    {

        var atp = GameObject.Find("HammyActor");
        var avoidThis = atp.transform.position;

 if (alive){
    var dist = Vector3.Distance(avoidThis, transform.position);

             if (dist < safeZone)

            Chase();

               else Rest();

            }

        }



    function Chase()

    {

    animation["ArmatureAction"].wrapMode = WrapMode.Loop;

    var target2 = GameObject.Find("HammyActor");

    var target = target2.transform.position;

    animation.CrossFade("ArmatureAction");

    transform.LookAt(target);

    transform.Translate(0,0,speed);

    }

function Rest()

    {

    animation["ArmatureAction"].wrapMode = WrapMode.Once;

        animation.CrossFade("ArmatureAction");

    }
more ▼

answered Jan 22 '12 at 02:51 PM

husbandofemily gravatar image

husbandofemily
94 6 9 12

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

x3342
x655
x55

asked: Jan 21 '12 at 04:35 PM

Seen: 591 times

Last Updated: Jan 22 '12 at 02:51 PM