Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 McStag · Aug 02, 2019 at 09:08 PM · multiplerandom.rangedifferentsynchronizing

How do I get multiple Random.ranges from one script

I'm trying to get a bunch of npc's to patrol randomly. I use Random.range to spawn a new position, and then i move the npc to that new position. I have the same script on all the NPC's, so they're using the same Random.Range, so they end up going to the same position. I'm using Random.Range to control how long they pause for inbetween moving, and again, they pause for the same amount of time. I think its important they use the same script, because I want to use lots of npc's in the one scene, and i dont want to make a new script for every npc. I'm really stumped. Is there a standard way of managing lots of randomness? I cant seem to find one.

Heres my script:

     private Animator animator;
     private SpriteRenderer spriteRenderer;
 
     public float speed;
     private float waitTime;
     public float startWaitTimeMin;
     public float startWaitTimeMax;
 
     public Transform moveSpot;
     public float minX;
     public float maxX;
     public float minY;
     public float maxY;
 
  
     void Start()
     {
         animator = GetComponent<Animator>();
         spriteRenderer = GetComponent<SpriteRenderer>();
 
         waitTime = Random.Range(startWaitTimeMin, startWaitTimeMax);
 
         moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
     }
 
     void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, 
         moveSpot.position, speed * Time.deltaTime);
 
 
         if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
         {
             if (waitTime <= 0)
             {
                 moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
                 animator.SetBool("isWalking", true);
                 waitTime = Random.Range(startWaitTimeMin, startWaitTimeMax);
             }
             else
             {
                 animator.SetBool("isWalking", false);
                 waitTime -= Time.deltaTime;
             }
         }
     }

Comment
Add comment · Show 2
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 Vega4Life · Aug 02, 2019 at 09:52 PM 1
Share

I would say something else is going on. Using Random.Range in Start() will produce different results - and in the Update (even if multiple gameObjects using the same script). if it didn't, that would be pretty broken.

avatar image McStag Vega4Life · Aug 02, 2019 at 11:49 PM 0
Share

You might be right, If i use a different moveSpot position for each NPC, they do seem to move differently.

I need to find somewhere to read up about how Random.range works lol

1 Reply

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

Answer by Bunny83 · Aug 03, 2019 at 01:33 AM

Random.Range will return a different random value each time it is called. However you seem to reference a gameobject with your "moveSpot" reference. Are you sure that each of your NPC has it's own Transform object? If they all use the same object, of course they will all move to the same position. When each NPC is setting the position of that one object, they will just overwrite each other and only the last assignment would take effect.


Why do you actually use a Transform? Since you only need a target location, you probably should simply store the target position in a Vector3 variable, so each NPC has it's own target. If for some reason you want / need to use an actual gameobject for the target position, make sure every NPC has it's own moveSpot gameobject and that each NPC is actually referencing their own.

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 McStag · Aug 03, 2019 at 03:16 AM 0
Share

I copied the majority of this script off of a YouTube tutorial, and have been playing around with it. I've been wondering about using another objects Transform, but hadn't got to it yet.

$$anonymous$$y first solution was to use different 'moveSpots' as there initial Transform, but it feels really clunky having to make new 'moveSpot' for each NPCs.

Thanks for the tip.

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

110 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

Related Questions

Use multiple NavMesh ? -1 Answers

(2D) How to shoot multiple bullets in different directions 1 Answer

How to add gameobject inside of an array? 0 Answers

Button functions change depending on holding down or tapping Button? 1 Answer

Xamarin opens multiple windows. 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