Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question Script for a time driven experience

Discussion in 'Scripting' started by jthiess, Nov 10, 2022.

  1. jthiess

    jthiess

    Joined:
    Nov 5, 2018
    Posts:
    31
    I am trying to build a passive digital exhibit that will be driven by a timer (time/duration will trigger the next event). The event should loop indefinitely, but likely 6-8 times per session. Below is the script I have come up with, that seems to work in theory, but I have no idea if it will run into issues after a few laps. I am also sure it is not optimized as this is really my first crack at this type of script. I would love any feedback or tips!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5.  
    6. public class timer : MonoBehaviour
    7. {
    8.     private float timeElapsed;
    9.  
    10.     // Duration of each scene (seconds)
    11.     private float titlePageTime = 2.0f;
    12.     private float themeOneTime = 5.0f;
    13.     private float themeTwoTime = 5.0f;
    14.     private float themeThreeTime = 5.0f;
    15.  
    16.     // Collections of objects
    17.     // May need to introduce additional carousels for certain scenes.
    18.     public GameObject carouselOne;
    19.     public GameObject carouselTwo;
    20.     public GameObject carouselThree;
    21.  
    22.     // Visual to keep track of what scene we are on
    23.     public bool TitleScene;
    24.     public bool Scene1;
    25.     public bool Scene2;
    26.     public bool Scene3;
    27.     public bool restart;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.         carouselOne.SetActive(false);
    33.         carouselTwo.SetActive(false);
    34.         carouselThree.SetActive(false);
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         timeElapsed += Time.deltaTime;
    41.  
    42.         if (timeElapsed >= titlePageTime)
    43.         {
    44.             scene1();
    45.         }
    46.         if (timeElapsed >= titlePageTime + themeOneTime)
    47.         {
    48.             scene2();
    49.         }
    50.         if (timeElapsed >= titlePageTime + themeOneTime + themeTwoTime)
    51.         {
    52.             scene3();
    53.         }
    54.         if (timeElapsed >= titlePageTime + themeOneTime + themeTwoTime + themeThreeTime)
    55.         {
    56.             resetScene();
    57.         }
    58.         Debug.Log(timeElapsed);
    59.     }
    60.     void title()
    61.     {
    62.         TitleScene = true;
    63.         Scene1 = false;
    64.         Scene2 = false;
    65.         Scene3 = false;
    66.         restart = false;
    67.  
    68.         carouselOne.SetActive(false);
    69.         carouselTwo.SetActive(false);
    70.         carouselThree.SetActive(false);
    71.     }
    72.     void scene1()
    73.     {
    74.         TitleScene = false;
    75.         Scene1 = true;
    76.         Scene2 = false;
    77.         Scene3 = false;
    78.         restart = false;
    79.  
    80.         carouselOne.SetActive(true);
    81.     }
    82.  
    83.     void scene2()
    84.     {
    85.         TitleScene = false;
    86.         Scene1 = false;
    87.         Scene2 = true;
    88.         Scene3 = false;
    89.         restart = false;
    90.  
    91.         carouselOne.SetActive(false);
    92.         carouselTwo.SetActive(true);
    93.     }
    94.     void scene3()
    95.     {
    96.         TitleScene = false;
    97.         Scene1 = false;
    98.         Scene2 = false;
    99.         Scene3 = true;
    100.         restart = false;
    101.  
    102.         carouselTwo.SetActive(false);
    103.         carouselThree.SetActive(true);
    104.     }
    105.  
    106.     void resetScene()
    107.     {
    108.         TitleScene = false;
    109.         Scene1 = false;
    110.         Scene2 = false;
    111.         Scene3 = false;
    112.         restart = true;
    113.  
    114.         // I'm not sure what the best approach would be to restart the time/scene
    115.  
    116.         // relaunch scene
    117.         string currentScene = SceneManager.GetActiveScene().name;
    118.         SceneManager.LoadScene(currentScene);
    119.  
    120.         // set timer back to 0
    121.         //timeElapsed = 0;
    122.     }
    123. }
    124.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Couldn't this all just be a single looping animation and you wouldn't have to write even one line of code?
     
  3. jthiess

    jthiess

    Joined:
    Nov 5, 2018
    Posts:
    31
    Potentially(?). I haven't used unity to make animations before.

    The exhibit is 30 or so 3D models made using photogrammetry. The models are groups into themes (scenes) and the camera moves into these scenes as the models become visible with their descriptions. It will be played on a 3D video wall we have which requires Unity to get the full 3D effect.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Super easy... you can even call methods (like your reset reloader) from animation callbacks.

    Make an empty game object

    put a cube under it, move it over
    put a sphere under it, move it the other way

    open the animation windown (cmd-6)

    select the empty object

    click add animation in the animation window (it will make a controller, an animation and add an animator to the root)

    press record in the animation window

    toggle the cube on / off
    toggle the sphere on / off

    now you have the first keyframe

    - set the cube off

    slide ahead in time

    turn the cube on
    turn the sphere off

    slide ahead in time

    turn the cube off
    turn the sphere on

    done... multiply it by 3 more things
     
  5. jthiess

    jthiess

    Joined:
    Nov 5, 2018
    Posts:
    31
    Okay, I will take a look at this option.

    For my own sake and coding growth, does the provided script make any sense?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Fair question... yes, it does potentially make sense... it's just for something like that it should be a collection (perhaps an array or list) of objects and an integer that steps along them, turning each one on while all the others off.

    spitballin' it here:

    Code (csharp):
    1. [Header( "Make sure you drag all items in here!")]
    2. public GameObject[] Things;
    and then:

    Code (csharp):
    1. void Update()
    2. {
    3.   // one thing every 5 second
    4.   int n = (int)(Time.time / 5.0f);
    5.  
    6.   // wrap endlessly here...
    7.   n %= Things.Length;
    8.  
    9.   // OR, if you don't wrap above:
    10.   if (n >= Things.Length)
    11.   {
    12.     // put your execute reload here
    13.     return;
    14.   }
    15.  
    16.   for (int i = 0; i < Things.Length; i++)
    17.   {
    18.     Things[i].SetActive( i == n);
    19.   }
    20. }
    That's what collections get you ... WAAAAAAAAAAAAAAY less code. :)

    Hit me if anything above looks wonky... no guarantees, I didn't run the above code, I just typed it.