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
0
Question by Ouija · Feb 18, 2015 at 03:58 AM · randomtimerpausewaypoint

Pause for random amount of time at waypoint

Hey guys, I have a script it works as is, but I can't seem to add a random pause at each waypoint. Basically right now he walks to each way point and moves on, I am trying to make him pause at each waypoint, Such as sometimes he will be standing there for 5 seconds, other times he might be standing there for 30 seconds before moving on. Any ideas? A timer of sorts? Also if you are a c# genius, I am trying to make him walk to a random waypoint instead of a set path. : 3

 void Patrolling ()
     {
         // Set an appropriate speed for the NavMeshAgent.
         nav.speed = patrolSpeed;
         
         // If near the next waypoint or there is no destination...
         if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance)
         {
             // ... increment the timer.
             patrolTimer += Time.deltaTime;
             
             // If the timer exceeds the wait time...
             if(patrolTimer >= patrolWaitTime)
             {
                 // ... increment the wayPointIndex.
                 if(wayPointIndex == patrolWayPoints.Length - 1)
                     wayPointIndex = 0;
                 else
                     wayPointIndex++;
                 
                 // Reset the timer.
                 patrolTimer = 0;
             }
         }
         else
             // If not near a destination, reset the timer.
             patrolTimer = 0;
         
         // Set the destination to the patrolWayPoint.
         nav.destination = patrolWayPoints[wayPointIndex].position;
 
     }
Comment
Add comment · Show 11
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 abi-kr01 · Feb 18, 2015 at 04:06 AM 1
Share

i think rendom.range will be perfect for your problem like

 int i= Rendom.Ranger(4,31) \\it will give rendom value from 4-30
  patrolTimer=i;

avatar image Ouija · Feb 18, 2015 at 04:09 AM 0
Share

Thanks man, very interesting thing to know. You up for showing me how it should look in the script? Before things get messy haha

avatar image Ouija · Feb 18, 2015 at 04:36 AM 0
Share

should this be in the update? or in the void patrolling?

     int i = Random.Range (4, 31);
avatar image abi-kr01 · Feb 18, 2015 at 05:44 AM 1
Share
     patrolWaitTime = Random.range(4,31);  
     and put it some where so it can be updated 
     or 
     int i= Random.range(4,31);
     patrolWaitTime = i;  //almost same thing 
 
 
 may be in above script so that every-time when gameobject goes from current to next waypoint it will have a new random value .
avatar image abi-kr01 · Feb 18, 2015 at 07:52 AM 1
Share

there you go :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by chadli · Feb 18, 2015 at 05:21 AM

There really are a million different ways to do it.

Look into the Random.Range documentation to see how to implement a number generator. See the link below.

http://docs.unity3d.com/ScriptReference/Random.Range.html

To pause you could use something like Random.Range(0, 10) and have the object wait through that range.

As far as the Waypoints go, try and play around with creating your own algorithm or look up anothers.

You could just have it visit adjacent nodes base on whether the random range returns a negative or positive and alternate between going up and down or side to side. It is really up to you.

Or you could do a range from 0-5 and have it go towards the index it needs to go. Does that make sense? In my example below O is the location of the object, and G is the goal of where you want to go. 0-5 would represent the spaces (1) that it can travel.

[1,O,1]

[1,1,1]

[1,G,1]

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 Ouija · Feb 18, 2015 at 05:41 AM 0
Share

I understand what you mean with the example you gave. I'm not exactly sure how to even begin to set that up tho haha. Bit of a noob

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

22 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

Related Questions

Adding a timer to spawner 2 Answers

Pause at end of game 2 Answers

How to add waypoints/ timers for a car game! 1 Answer

Random spawning in open environment 1 Answer

Pause Menu upon Idle (timer & button issues C#) 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