Guest User

SimpleCinematicScript

a guest
Jun 20th, 2014
682
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SimpleCinematicScript : MonoBehaviour {
  5.  
  6.     CameraFollow cFollow;
  7.     PlayerMove pMove;
  8.    
  9.     public Transform Target1;
  10.     public Transform Target2;
  11.    
  12.     public Vector3 Target1Offset;
  13.     public Vector3 Target2Offset;
  14.    
  15.     public float Target1FollowSpeed;
  16.     public float Target2FollowSpeed;
  17.    
  18.     public float DelayStart=0;
  19.     public float ShowTarget1ThisLong=0;
  20.     public float ShowTarget2ThisLong=0;
  21.    
  22.     private Transform OriginalTarget;
  23.     private Vector3 OriginalTargetOffset;
  24.     private float OriginalTargetFollowSpeed;
  25.    
  26.     public bool OnlyTriggerOnce=true;
  27.        
  28.     //Cinematic routine! It'll change our values of the camera follow one at a time
  29.     IEnumerator StartCinematic()
  30.     {
  31.         //Let's wait a delay amount of time to start.
  32.         yield return StartCoroutine(Wait(DelayStart));
  33.        
  34.         //Let's disable our player movement and set our rigidbody to kinematic so it won't move...
  35.         pMove.enabled=false;
  36.         pMove.gameObject.GetComponent<Rigidbody>().isKinematic=true;
  37.        
  38.         //Let's set our followspeed, target and target offset to our first cinematic target.
  39.         cFollow.followSpeed=Target1FollowSpeed;
  40.         cFollow.target=Target1;
  41.         cFollow.targetOffset=Target1Offset;
  42.        
  43.         //wait a bit
  44.         yield return StartCoroutine(Wait(ShowTarget1ThisLong));
  45.        
  46.         //Set to our second target
  47.         cFollow.followSpeed=Target2FollowSpeed;
  48.         cFollow.target=Target2;
  49.         cFollow.targetOffset=Target2Offset;
  50.        
  51.         //wait a bit more
  52.         yield return StartCoroutine(Wait(ShowTarget2ThisLong));
  53.        
  54.         //Reset to the values we had before starting the cinematic.
  55.         cFollow.followSpeed=OriginalTargetFollowSpeed;
  56.         cFollow.target=OriginalTarget;
  57.         cFollow.targetOffset=OriginalTargetOffset;
  58.        
  59.         //Enable the player movement back, and set the rigid body iskinematic to false so the rigid body will work again.
  60.         pMove.enabled=true;
  61.         pMove.gameObject.GetComponent<Rigidbody>().isKinematic=false;
  62.        
  63.         //Now, if we want to trigger this again, and not only once...
  64.         if(!OnlyTriggerOnce){
  65.             //We'll wait 5f seconds
  66.             yield return StartCoroutine(Wait(5f));
  67.             //and re-enable the trigger :D
  68.             gameObject.collider.enabled=true;
  69.         }
  70.     }
  71.    
  72.     //This is a fancy wait coroutine for the others to use.
  73.     IEnumerator Wait(float duration)
  74.     {
  75.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  76.             yield return 0;
  77.     }
  78.    
  79.     //We should trigger if the player enters our trigger
  80.     void OnTriggerEnter(Collider other) {
  81.         //If what entered our trigger has the TAG Player...
  82.         if(other.gameObject.tag=="Player") {
  83.             //Let's ask the Camera to give us their Camera Follow component to mess with!
  84.             cFollow = Camera.main.GetComponent<CameraFollow>();
  85.             //Let's set the original values that are there right now so we can get back to it later.
  86.             OriginalTarget=cFollow.target;
  87.             OriginalTargetOffset=cFollow.targetOffset;
  88.             OriginalTargetFollowSpeed=cFollow.followSpeed;
  89.             //Also let's set our PlayerMove Script to be the Player that just touched us. So we can disable him.
  90.             pMove=other.gameObject.GetComponent<PlayerMove>();
  91.             //And let's turn this trigger off because... I don't want this being triggered while it's playing the cinematic
  92.             //If that happens I'll be in trouble since I won't have the original values anymore
  93.             gameObject.collider.enabled=false;
  94.             //And finally... Let's start the Cinematic!
  95.             StartCoroutine(StartCinematic());
  96.         }
  97.     }
  98. }
RAW Paste Data