Search Unity

Detect Certain amount of given time

Discussion in 'Scripting' started by gladiuscloud, Feb 20, 2018.

  1. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    How do I implement a method to do something like this in my game:

    My Hero needs to eat food within 5 sec. or else my animation will change to hungry..
    Is there anyway I can detect if it reaches to 5 sec. before I'll change the animation to hungry?
     
  2. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    Need to loop back again to count for 5 sec. to eat food again.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Add a "float hungryTime" property to your script. This represents the time (i.e. Time.time value) at which your character will be hungry again.

    When he eats something, set that property to Time.time + 5 (or whatever you feel is appropriate).

    Then in your Update method, use a simple if statement... if (Time.time > hungryTime), then change your animation or do whatever else you do in response to hunger.
     
    gladiuscloud likes this.
  4. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    I will try this one. Then return back for result.. thank you.
     
  5. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    it works perfectly!
     
    JoeStrout likes this.