Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Nekobolo · Feb 10, 2017 at 07:03 AM · c#2d2d-platformerenemy ai

Random Number keeps generating over and over

Hi! I have a script I'm trying to make work. The issue currently is that after the first random pick, it keeps picking again and again without any delay, thus making the object have a seizure. The script:

     public void RandomDelay()
     {
         RandomPick = Random.Range(1, 4);
         Debug.Log("NEW RANDOM PICKED");
     }

      if (RandomPick == 1)
     {
         transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
         Debug.Log("RANDOM CHOICE 1");
         InvokeRepeating ("RandomDelay", 3.0f, 3.0f);
     }
     
     if (RandomPick == 2)
     {
         transform.position = Vector2.MoveTowards(transform.position, -target.position, speed * Time.deltaTime);
         Debug.Log("RANDOM CHOICE 2");
         InvokeRepeating ("RandomDelay", 3.0f, 3.0f);
     }
     
     if (RandomPick == 3)
     {
         transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime * 0); //Doesn't move
         Debug.Log("RANDOM CHOICE 3");
         InvokeRepeating ("RandomDelay", 3.0f, 3.0f);
     }        

If anyone has any ideas that could make this work properly, I'd love to hear them!

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

2 Replies

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

Answer by Nekobolo · Feb 10, 2017 at 08:52 AM

Fixed it by adding a CancelInvoke at the end of the Random Delay method.

     public void RandomDelay()
     {
         RandomPick = Random.Range(1, 4);
         Debug.Log("NEW RANDOM PICKED");
         CancelInvoke("RandomDelay");
     }
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 Happy-Zomby · Feb 10, 2017 at 08:33 AM

Hi, invoke repeating means you keep calling a function... and it keeps repeating ... therefore in your case your are calling a new repeat session every 3 seconds without cancelling the previous sessions... so you will quickly have hundreds of instance of this formula repeating.

 //change:
 
 InvokeRepeating ("RandomDelay", 3.0f, 3.0f);
 
 //to:
 
 StartCoroutine("Repick");
 
 //and then
 IEnumerator DelayedAnimation ()
     {
         yield return new WaitForSeconds(3.0f);
        RandomDelay();
     }
 

also - your item won't move if you don't put the transform position in a void update() .... so use your randomdelay to assign a destination to a vector 3 variable and then movetowards that variable in your update part.

hope that helps,

Comment
Add comment · Show 3 · 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 Happy-Zomby · Feb 10, 2017 at 08:37 AM 0
Share

here is the page for moveTowards https://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html see how it is in Update() part you can assign public Transform target; in your randomdelay function

avatar image Nekobolo · Feb 10, 2017 at 08:41 AM 0
Share

So I changed it to

 if (RandomPick == 3)
             {
                 transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime * 0); //Doesn't move
                 Debug.Log("RANDO$$anonymous$$ CHOICE 3");
                 StartCoroutine("Repick");
             }    
     
     
         IEnumerator Repick ()
             {
                 yield return new WaitForSeconds(3.0f);
                 RandomDelay();
             }

but the same issue is occurring, it just keeps generating new values for the random variable.

avatar image Happy-Zomby Nekobolo · Feb 10, 2017 at 08:57 AM 0
Share

it won't move as long as you don't put the moveTowards in an update function also I'm not sure how you are getting your target.position.... see the link I sent for movetowards

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

329 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have multiple enemies (currently having to kill them in order) 1 Answer

How to detect if ObjectA is looking at ObjectB, and vice versa? 2 Answers

Trouble with directing launched projectiles in Unity 2D 0 Answers

How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers

Help with enemy AI Please! (on 2D Platformer) 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