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 duruenesqr · May 22, 2019 at 12:03 AM · aitimebehaviourcyclenpcs

How to make NPCs check the time?

Hi, I am trying to create a 2d game with a town in which some NPCs have a schedule. Just like Skyrim, the town is a scene and interiors are separate scenes, like tavern is one scene etc. I tried to learn the logic through some research and found some information about it. People say that you need to make the NPC check the time and do its actions according to it. So how am I supposed to make the NPC check the time of day?

I mean, how can I create the "thing" that acts as the "time" and make all NPCs look at it before being where they are supposed to be and do their routine actions?

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
Best Answer

Answer by Romano · May 22, 2019 at 12:54 AM

There's a few ways you could do this but I would use a coroutine to do it. That's the thing that acts as the time. It's like a function you can make repeatedly loop through a set amount of time and trigger events at set intervals. Imagine a game where 24 hours last just 24 minutes, you could set it up the coroutine like this:

 public IEnumerator DayCoroutine()
 {
     // The timer
     float elapsedTime = 0;
     // 24 minutes expressed in seconds
     float twentyFourMinsInSeconds = 60 * 24;
     // 7 minutes in seconds
     float sevenAM = 60 * 7;
     // 9 minutes in seconds
     float nineAM = 60 * 9;
     // 18 minutes in seconds
     float sixPM = 60 * 18;
     // 22 minutes in seconds
     float tenPM = 60 * 22;
 
     bool wokeUp = false;
     bool wentToWork = false;
     bool wentHome = false;
     bool wentToSleep = false;
 
     // Loop forever
     while (true)
     {
         // Reset the timer to zero
         elapsedTime = 0;
 
         // Reset the bools for a new day
         wokeUp = false;
         wentToWork = false;
         wentHome = false;
         wentToSleep = false;
 
         while (elapsedTime < twentyFourMinsInSeconds)
         {
             if (wokeUp == false && elapsedTime > sevenAM)
             {
                 // A function that wakes your character up
                 WakeUp();
                 // Set the bool to true so we can stop checking this.
                 wokeUp = true;
             }
             else if (wentToWork == false && elapsedTime > nineAM)
             {
                 // A function that wakes your character up
                 GoToWork();
                 // Set the bool to true so we can stop checking this.
                 wokeUp = true;
             }
             else if (/*  not come home by 6 pm then come home */)
             else if (/* not asleep by 10 pm then go to sleep */)
 
             // Progress the coroutine to the next frame.
             yield return null;
         }
 
         yield return null;
     }
 

And you can trigger the coroutine from the Start function like this:

 void Start()
 {
     StartCoroutine(DayCoroutine();
 }

That's not the tidiest example, but basically something along those lines should work.

Comment
Add comment · Show 2 · 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 duruenesqr · May 22, 2019 at 07:15 AM 0
Share

Thank you very much for your answer. It helped me get the logic better.

In addition, do you think that it can be done via behaviour trees? like third party asset-plugins?

avatar image Romano duruenesqr · May 22, 2019 at 08:26 AM 0
Share

I've never used anything like that, but I guess it depends on the complexity of your project. Something that uses the time to trigger functions is relatively straight forward. An alternative approach would be to add the timer to the NPC and time how long they have been in an area/doing a task for.

So the NPC script would look more like:

 WakeUp()
 (Wait for 2 hours using a coroutine)
 GoToWork()
 (Wait for 8 hours or for another source to tell the coroutine to stop - e.g. the NPC can't continue to work because the building has burnt down)
 GoHome()

Generally I avoid third party assets unless I really need them, because they often come with their own complexities and other things to learn. The state machines in the mecanim animator in Unity can be used for things like this, where the NPC animation or set of animations will loop until something external tells it otherwise or you can use a State$$anonymous$$achineBehaviour script to control behaviour.

I really like this tutorial as an introduction to how to use State$$anonymous$$achineBehaviours:

Animation Behaviours

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

177 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

Related Questions

Cycle Through Array of Enemies 1 Answer

Physics behaves more strangely with Time? Bouncing and Sliding Objects 2 Answers

Animator/Behaviour I want to get all the transitions of a state 0 Answers

A.I That tracks the player then resets 1 Answer

Am I using Behavior Designer correctly to control the AI of my character? 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