Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scripting "scripted" Events.

Discussion in 'Scripting' started by TheCowMan, May 1, 2014.

  1. TheCowMan

    TheCowMan

    Joined:
    Aug 24, 2010
    Posts:
    387
    How would one go about scripting "cut scenes" that aren't really animated cut scenes? Let's say I wanted a person to click on a button, something to happen (a tutorial) and in that tutorial have a bunch of things moving on their own?

    Example:
    This arrow goes here to show this person where to click for this.
    After clicking that, the game "Plays itself" to show the player how the game mechanics work
    Then the player must repeat that to show they understand
    etc, etc.

    Would I just have to write one very specific script and write out each event and trigger them when I need to? Or is there an easier way than writing a long script?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Write a scripted-event editor... and leave the code behind. :)

     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Coroutines are the best answer for this, probably. You can do any arbitrary scripting action, then wait for any arbitrary condition, then do more things. It'd take the general form of something like this:

    Code (csharp):
    1.  
    2. void Start() {
    3. StartCoroutine(Tutorial());
    4. }
    5.  
    6. public IEnumerator Tutorial() {
    7. tutorialText.text = "Welcome to the tutorial! Press X";
    8. while (!Input.GetKeyDown("x")) yield return null;
    9. tutorialText.text = "Good job! Now move to position (0, 10, 0)";
    10. while (Vector3.Distance(playerObject, new Vector3(0f,10f,0f)) > 0.5f) yield return null;
    11. tutorialText.text = "And so on";
    12. }
    13.  
     
  4. TheCowMan

    TheCowMan

    Joined:
    Aug 24, 2010
    Posts:
    387
    Thanks for the replies guys! LightStriker, that seems like a lot more work for now, so i'll go with the cop out solution and do it by code :p
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We use PlayMaker. It's a finite state machine that's consistently a top 3 seller in the Unity Assets store.
    http://www.hutonggames.com/

    I would recommend using finite state machines over co-routines. Co-routines are harder to debug as you can have a hundred of them running at once. A finite state machine makes sure only one thing is happening at a time.