SplashSlider

This code controls the different title screen elements sliding on screen. There are several title pieces, buttons, and Frothy Bird's head. Each comes in and an 8-bit sword clash sound plays.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Assets.Scripts.States;

public class SplashSlider : MonoBehaviour
{

    public List<GameObject> slidingElements = new List<GameObject>();    //Contains each element that will be sliding in.
    public List<Vector3> startingPositions = new List<Vector3> ();      //Contains starting position of each element.
    public List<Vector3> finalPositions = new List<Vector3> ();            //Contains final position of each element.
    private int slidingCounter;
    public bool sliding;
    public bool readyToSlide;
    private float distCovered; 
    public float speed;
    private float startTime;
    private float totalDistance;
    private float fractionOfDistance;

    private AudioSource splashAudioSource;
    public AudioClip slideCompleteSound;
    private StateManager stateManager;
    private SplashController splashController;


    void Awake ()  //Changed from start to awake to ensure the save file is loaded before anything else for achievements, etc.
    {
        Time.timeScale = 1.0f;
        stateManager = GameObject.Find ("StateManager").GetComponent<StateManager> ();
        splashAudioSource = gameObject.GetComponent<AudioSource> ();
        splashController = GameObject.Find ("SplashContainer").GetComponent<SplashController> ();

        SaveLoad.Load ();  //Load the save data when the main game is firing up. 
        GameData.current = SaveLoad.savedGame;

        //For case that game has never been played before, so continue button will not be visible or clickable.
        if ((GameData.current.highLevel < 2)) 
        {
            GameObject.Find ("ContinueButton").GetComponent<SpriteRenderer>().enabled = false;
            GameObject.Find ("ContinueButton").GetComponent<BoxCollider2D>().enabled = false;
        }
        foreach (GameObject go in slidingElements)
        {
            startingPositions.Add (go.transform.position);
        }
        slidingCounter = 0;
    }

    void Update () 
    {

        if(readyToSlide && (slidingCounter < slidingElements.Count))
        {
            readyToSlide = false;
            StartCoroutine (SlideElement (slidingElements[slidingCounter]));
        }

        if (slidingCounter >= slidingElements.Count && stateManager.stateString == "BeginState" && stateManager.subStateString != "TitleMenuSubState") 
        {
            stateManager.SwitchSubState(new TitleMenuSubState(stateManager));
        }
    
    }

    IEnumerator SlideElement(GameObject slidingObject)
    {
        sliding = true;
        fractionOfDistance = 0.0f;
        distCovered = 0.0f;
        Vector3 startPosition = startingPositions [slidingCounter];
        Vector3 finalPosition = finalPositions [slidingCounter];

        totalDistance = Mathf.Sqrt (Mathf.Pow ((finalPosition.x - startPosition.x),2)+Mathf.Pow ((finalPosition.y - startPosition.y),2));

        slidingCounter += 1;

        while (sliding)
        {

            distCovered = speed * (Time.deltaTime) + distCovered; //speed time the total time passed so far.
            fractionOfDistance = distCovered/totalDistance;
            slidingObject.transform.position = Vector3.Lerp(startPosition, finalPosition, fractionOfDistance);
            //Check to see if x and y are close enough to position or user has clicked, tapped, whatever to skip sliding in stuff.
            if ((Mathf.Abs (fractionOfDistance) > 0.99) || splashController.skipSliding)
            {
                sliding = false;
                slidingObject.transform.position = finalPosition;
                splashAudioSource.clip = slideCompleteSound;
                splashAudioSource.Play ();
            }
            yield return 0;
        }
        readyToSlide = true;
        yield return 0;

    }