Advertisement
Guest User

HazardHelper

a guest
Jun 20th, 2014
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class HazardHelper : MonoBehaviour {
  5.  
  6.     //This is our baby! Our little hazard!
  7.     public GameObject myHazard;
  8.    
  9.     //We'll use those vectors to store our old and new positions
  10.     private Vector3 HazardOriginalPosition;
  11.     private Vector3 HazardNewPosition;
  12.    
  13.     //This will control the actual movement on the update function
  14.     private bool Appearing;
  15.     private bool Disappearing;
  16.     //We need this bool to make sure we don't trigger twice before resetting
  17.     private bool Busy = false;
  18.    
  19.     //How high should I go?
  20.     public float NewHazardY=1f;
  21.  
  22.     //How long after a player has passed by should we trigger to go up?
  23.     public float TimeToTrigger=1f;
  24.     //How fast should we go up?
  25.     public float RaiseSpeed=0.2f;
  26.     //How much time until we go down?
  27.     public float TimeToGoDown=6f;
  28.     //How fast should we go down?
  29.     public float GoDownSpeed=0.2f;
  30.    
  31.     //How much time until we come back?
  32.     public float TimeToTriggerIndependentOfPlayer=3f;
  33.     //If we want this to be appearing and disappearing independent of player contact.
  34.     public bool PlayerIndependent = false;
  35.    
  36.     //It's the start of the game!
  37.     void Start() {
  38.         //If you don't have a hazard for this, it's not going to work
  39.         if (!myHazard) {
  40.             Debug.Log("No, you need a hazard, seriously, this won't work without one!");
  41.             return;
  42.         }
  43.         //We need to store our hazard positions when it's down and when its up
  44.         HazardOriginalPosition=myHazard.transform.position;
  45.         HazardNewPosition=HazardOriginalPosition;
  46.         HazardNewPosition.y=NewHazardY;
  47.         //If we're player independent...
  48.         if(PlayerIndependent) {
  49.             //We should start counting to trigger
  50.             StartCoroutine(PlayerIndependentTriggering());
  51.         }
  52.     }
  53.    
  54.     //This is called every frame
  55.     void Update() {
  56.         //If we are not appearing nor disappearing, stop here.
  57.         if(!Appearing&&!Disappearing) {
  58.             return;
  59.         }
  60.         //If I do have a Hazard...
  61.         if(myHazard) {
  62.             //If I'm appearing
  63.             if(Appearing) {
  64.                 //We'll do a Lerp to find out the next Y position for us to move smoothly
  65.                 float currentY = Mathf.Lerp(myHazard.transform.position.y,NewHazardY,RaiseSpeed);
  66.                 //We'll create a new vector3 for our hazard based on the current Y we found.
  67.                 Vector3 NewPosition = new Vector3 (myHazard.transform.position.x,currentY,myHazard.transform.position.z);
  68.                 //We'll set the hazard's transform to that new position we found
  69.                 myHazard.transform.position=NewPosition;
  70.             }
  71.             //If I'm disappearing
  72.             if(Disappearing) {
  73.                 //We'll do a Lerp to find out the next Y position for us to move smoothly
  74.                 float currentY = Mathf.Lerp(myHazard.transform.position.y,HazardOriginalPosition.y,GoDownSpeed);
  75.                 //We'll create a new vector3 for our hazard based on the current Y we found.
  76.                 Vector3 NewPosition = new Vector3 (myHazard.transform.position.x,currentY,myHazard.transform.position.z);
  77.                 //We'll set the hazard's transform to that new position we found
  78.                 myHazard.transform.position=NewPosition;
  79.             }
  80.         }
  81.     }
  82.    
  83.     //This coroutine will wait to trigger then bring the hazard up
  84.     IEnumerator DoTheHazardBringing()
  85.     {
  86.         yield return StartCoroutine(Wait(TimeToTrigger));
  87.         Appearing=true;
  88.         //If the developer wants it to come back (TimeToComeBack bigger than 0...)
  89.         if(TimeToGoDown>0) {
  90.             //Let's start our ComeBack Routine
  91.             StartCoroutine(ComeBack());
  92.         }
  93.         yield return StartCoroutine(Wait(RaiseSpeed*2f));
  94.         Appearing=false;
  95.     }
  96.    
  97.     //This one is in case you want this to go on and off constantly without player interaction
  98.     IEnumerator PlayerIndependentTriggering()
  99.     {
  100.         //It'll wait for the Time To Disappear time, than call the Hazard bringing coroutine.
  101.         yield return StartCoroutine(Wait(TimeToTriggerIndependentOfPlayer));
  102.         yield return StartCoroutine(DoTheHazardBringing());
  103.     }
  104.    
  105.     //Come back routine! Like the appearing one but opposite!
  106.     IEnumerator ComeBack()
  107.     {
  108.         //It'll wait for the Time To ComeBack time, then start to disappear.
  109.         yield return StartCoroutine(Wait(TimeToGoDown));
  110.         Disappearing=true;
  111.         //If we're player independent...
  112.         if(PlayerIndependent) {
  113.             //We should start counting to appear again.
  114.             StartCoroutine(PlayerIndependentTriggering());
  115.         }
  116.         yield return StartCoroutine(Wait(GoDownSpeed*2f));
  117.         Disappearing=false;
  118.         //We should know we're not busy anymore, we can trigger again :D
  119.         Busy=false;
  120.     }
  121.  
  122.     //This is a fancy wait coroutine for the others to use.
  123.     IEnumerator Wait(float duration)
  124.     {
  125.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  126.             yield return 0;
  127.     }
  128.  
  129.     //We should trigger if the player enters our trigger
  130.     void OnTriggerEnter(Collider other) {
  131.         //If we're player independent, just stop this D:
  132.         if(PlayerIndependent||Busy) {
  133.             return;
  134.         }
  135.         if(other.gameObject.tag=="Player") {
  136.             Busy=true;
  137.             StartCoroutine(DoTheHazardBringing());
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement