Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by husbandofemily · Jan 21, 2012 at 04:35 PM · enemyrandom.range

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 ;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
       
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges