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 jiak1 · Dec 13, 2017 at 02:35 AM · navigationrandomization

Unity Random.Range Issue

I am currently implementing a sheep AI into my game but pretty soon they all choose to go to the same spot, i can't for the life of me work out what is wrong as i am reading the output for the random number and it works fine for about 20 seconds then it just defaults to 94..... Just for clarification i have a master gameobject with about 150 children that are possible points for the ai to go to. Any help is appreciated XD

A Video Of The Issue Can Be Found Here: https://youtu.be/J1Eo3SoXsbw

My Sheep Controller Code:

     enum PossibleActions { Idle, Eating, Walking, RunningForExit }
     PossibleActions currentAction;
 
     GameObject locations;
     NavMeshAgent agent;
     public float timeLeftOfIdle, timeLeftOfWalking, timeLeftOfEating = 10f;
 
     public bool doAi = false;
     public bool doAnim = true;
 
     public bool forceMove = false;
     public Animator anim;
 
     // Use this for initialization
     void Start()
     {
         if (!(GameInfo.Instance.isHost)) { GetComponent<NavMeshAgent>().enabled = false; this.enabled = false; }
         anim = GetComponentInChildren<Animator>();
         locations = GameInfo.Instance.SheepNavigationLocations;
         agent = GetComponent<NavMeshAgent>();
 
         Debug.Log("Start Method Calling For " + transform.name);
         currentAction = pickAction(true);
         pickLocation();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (doAi)
         {
             if (currentAction == PossibleActions.Eating)
             {
                 timeLeftOfEating -= Time.deltaTime;
                 if (timeLeftOfEating <= 0.1f)
                 {
                     //Eating time limit hit pick a new task
                     currentAction = pickAction(forceMove);
                 }
             }
             else if (currentAction == PossibleActions.Idle)
             {
                 timeLeftOfIdle -= Time.deltaTime;
                 if (timeLeftOfIdle <= 0.1f)
                 {
                     //Idle time limit hit pick a new task
                     currentAction = pickAction(forceMove);
                 }
             }
             else if (currentAction == PossibleActions.Walking)
             {
 
                 timeLeftOfWalking -= Time.deltaTime;
                 if (timeLeftOfWalking <= 0.1f)
                 {
                     //Walking time limit hit pick a new task
                     currentAction = pickAction(forceMove);
                 }
                 else if (!agent.pathPending)
                 {
                     if (agent.remainingDistance <= agent.stoppingDistance)
                     {
                         if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
                         {
                             //Reached destination pick a new task
                             Debug.Log(transform.name + " Sheep Reached Its Destination");
                             currentAction = pickAction(forceMove);
                         }
                     }
                 }
             }
             else if (currentAction == PossibleActions.RunningForExit)
             {
                 currentAction = pickAction(forceMove);
             }
         }
     }
 
     PossibleActions pickAction(bool forceMove = false)
     {
         timeLeftOfWalking = Random.Range(5, 25);
         agent.SetDestination(pickLocation());
         WalkAnim();
         Debug.Log("Walking For " + transform.name + "Forcemove: " + forceMove);
         return PossibleActions.Walking;
   
     }
 
     Vector3 pickLocation()
     {
         int childNum = Random.Range(0, locations.transform.childCount - 1);
         Debug.Log("Picked Num:" + childNum);
         return locations.transform.GetChild(childNum).transform.position;
     }
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 theterrificjd · Dec 13, 2017 at 10:02 AM

One of your target locations may be out of reach (under the ground, possibly). Make sure they are all accessible locations. You can also change your destination check to trigger within a certain range of the target location to adjust for this possibility and make it so the sheep don't all touch the same exact spots. (or for added randomization, offset the x and y randomly as well).

You can also add a timeout function like your other ones to grab another target if it takes them too long, as a fail safe.

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 jiak1 · Dec 17, 2017 at 07:50 AM 0
Share

That still doesn't explain why it randomly generates the number 94 so much. Thanks anyway :) i checked and changing the locations didn't seem to help.

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

119 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

Related Questions

Off Mesh Link - Limiting access to certain agents 2 Answers

Best way of creating unity car ai that wonders around searching for player 1 Answer

Augmented Reality Navigation 0 Answers

Nav Mesh Agent radius next to my game object causing a deviation. 0 Answers

Black bar of navigation bar position on Android 8.1 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