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 /
avatar image
0
Question by Zexstoi · Dec 21, 2017 at 03:32 PM · unity 5movementnot workingnot-workingrng

How to stop a Random Number Generator

Hi programmers around the world I am having a problem with a RNG. I am starting the random number generator but the problem is that it runs forever and I really need to stop it. Here are the details I am having a class called

private void RNGvoid() { int number = UnityEngine.Random.Range(0, 9); randomNumber = number; }

I am using this class in a Coroutine which looks like this:

private IEnumerator RNG() { RNGvoid(); yield return new WaitForSeconds(0); }

That Coroutine I am using in order to move my enemy in random directions just like in a MMO RPG game where an enemy just goes around in random directions around its spawnPoint. The problem is that beacuse of the RNG it tries to go in one direction one second and the next second it tries to go in another direction so can someone help by telling me how to stop the RNG or maybe help me with some checks in the method where I move the enemy in a random direction. Here is my code for moving the enemy:

private void EnemyPositioning() { StartCoroutine(RNG()); if (randomNumber == 0) { transform.position = Vector3.MoveTowards(transform.position, respawnPoint.position, speed * Time.deltaTime); } if (randomNumber == 1) { transform.position = Vector3.MoveTowards(transform.position, respawnPoint.GetChild(0).position, speed * Time.deltaTime); } }

Remember the goal here is to: Either find a way to stop the RNG after generation a number and then starting again or find a way to get the enemy from its current position to the position referred to by the random number.

Note there to little if statements in the EnemyPositioning() method because there is really no use of making the others without making these 2 work.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by villevli · Dec 21, 2017 at 04:47 PM

You should check if your enemy has reached the target position before selecting a new target position. You don't need a coroutine for this, you can just use Update(). You should not write duplicate code and it is easy to avoid in this case.

Just call this in Update(). All children of respawnPoint are assumed to be waypoints:

 Transform target = respawnPoint.GetChild(randomNumber);

 // go towards target
 transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 
 // check if we have reached the target and select a new target
 if (Vector3.Distance(transform.position, target.position) < 0.01f)
 {
     randomNumber = UnityEngine.Random.Range(0, respawnPoint.childCount);
 }
Comment
Add comment · Show 1 · 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 Zexstoi · Dec 21, 2017 at 05:24 PM 0
Share

@villevli Thank you so much for your help It works better than what I had but I still have some issues the enemy gets stuck in a position for some reason. What happens is that it start going from position to position but when it transition sometimes he transitions to another random position and other times it just gets stuck in the same position since its your code I am using you for further assistance Thanks for all your help so far it really means a lot!

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

178 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

Related Questions

Unable to launch unity 5.6.x in MacOS big sur 1 Answer

Character rotation and move to mouse click point with speed 0 Answers

Unity Won't Build Android 0 Answers

ENEMY MOVEMENT SCRIPT 0 Answers

player not moving over the bridge 1 Answer


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