- Home /
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!
Answer by Lo0NuhtiK · Jan 22, 2012 at 02:05 AM
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 ;
}
Answer by husbandofemily · Jan 22, 2012 at 02:51 PM
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");
}
Answer by husbandofemily · Jan 22, 2012 at 02:51 PM
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;
}
Your answer
Follow this Question
Related Questions
Enemy following Player on uneven surface 1 Answer
Problem with the enemy 0 Answers
Points when an enemy dies. 2 Answers
NEED HELP WITH ENEMY AI SCRIPT 2 Answers
Scripts for enemies ! 0 Answers