Advertisement
Guest User

UnstablePlatform

a guest
Jun 20th, 2014
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UnstablePlatform : MonoBehaviour {
  5.  
  6.     //We'll need to turn off and on our renderer for this
  7.     Renderer PlatformMesh;
  8.    
  9.     //How much time until we disappear?
  10.     public float TimeToDisappear=1f;
  11.     //How much time until we come back?
  12.     public float TimeToComeBack=6f;
  13.    
  14.     //How much time until we come back?
  15.     public float TimeToDisappearIndependentOfPlayer=3f;
  16.     //If we want this to be appearing and disappearing independent of player contact.
  17.     public bool PlayerIndependent = false;
  18.    
  19.     //It's the start of the game!
  20.     void Start() {
  21.         //Let's get a reference to our renderer so we can turn it on and off
  22.         PlatformMesh = GetComponent<Renderer>();
  23.         //If we're player independent...
  24.         if(PlayerIndependent) {
  25.             //We should start counting to disappear
  26.             StartCoroutine(PlayerIndependentDisappearing());
  27.         }
  28.     }
  29.    
  30.     //This coroutine will do the blinking over time
  31.     IEnumerator DoTheBlinking()
  32.     {
  33.         //Start false, wait for X time, than go true, and then false, and...
  34.         PlatformMesh.enabled=false;
  35.         yield return StartCoroutine(Wait(TimeToDisappear*0.1f));
  36.         PlatformMesh.enabled=true;
  37.         yield return StartCoroutine(Wait(TimeToDisappear*0.3f));
  38.         PlatformMesh.enabled=false;
  39.         yield return StartCoroutine(Wait(TimeToDisappear*0.2f));
  40.         PlatformMesh.enabled=true;
  41.         yield return StartCoroutine(Wait(TimeToDisappear*0.1f));
  42.         PlatformMesh.enabled=false;
  43.         yield return StartCoroutine(Wait(TimeToDisappear*0.05f));
  44.         PlatformMesh.enabled=true;
  45.         yield return StartCoroutine(Wait(TimeToDisappear*0.025f));
  46.         PlatformMesh.enabled=false;
  47.         yield return StartCoroutine(Wait(TimeToDisappear*0.02f));
  48.         PlatformMesh.enabled=true;
  49.         yield return StartCoroutine(Wait(TimeToDisappear*0.01f));
  50.         PlatformMesh.enabled=false;
  51.         //Now that we're finally gone, turn collider off
  52.         gameObject.collider.enabled=false;
  53.         //If the developer wants it to come back (TimeToComeBack bigger than 0...)
  54.         if(TimeToComeBack>0) {
  55.             //Let's start our ComeBack Routine
  56.             StartCoroutine(ComeBack());
  57.         }
  58.     }
  59.    
  60.     //This one is in case you want this to go on and off constantly without player interaction
  61.     IEnumerator PlayerIndependentDisappearing()
  62.     {
  63.         //It'll wait for the Time To Disappear time, than call the DoTheBlinking coroutine.
  64.         yield return StartCoroutine(Wait(TimeToDisappearIndependentOfPlayer));
  65.         yield return StartCoroutine(DoTheBlinking());
  66.     }
  67.    
  68.     //Come back routine! Like the disappearing one but opposite!
  69.     IEnumerator ComeBack()
  70.     {
  71.         //It'll wait for the Time To ComeBack time, than blink a bit.
  72.         yield return StartCoroutine(Wait(TimeToComeBack));
  73.         yield return StartCoroutine(Wait(TimeToDisappear*0.1f));
  74.         PlatformMesh.enabled=true;
  75.         yield return StartCoroutine(Wait(TimeToDisappear*0.3f));
  76.         PlatformMesh.enabled=false;
  77.         yield return StartCoroutine(Wait(TimeToDisappear*0.2f));
  78.         PlatformMesh.enabled=true;
  79.         yield return StartCoroutine(Wait(TimeToDisappear*0.1f));
  80.         PlatformMesh.enabled=false;
  81.         yield return StartCoroutine(Wait(TimeToDisappear*0.05f));
  82.         PlatformMesh.enabled=true;
  83.         //And now we enable our collider again.
  84.         gameObject.collider.enabled=true;
  85.         //If we're player independent...
  86.         if(PlayerIndependent) {
  87.             //We should start counting to disappear again.
  88.             StartCoroutine(PlayerIndependentDisappearing());
  89.         }
  90.     }
  91.  
  92.     //This is a fancy wait coroutine for the others to use.
  93.     IEnumerator Wait(float duration)
  94.     {
  95.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  96.             yield return 0;
  97.     }
  98.  
  99.     //We should blink and disappear if the player collides with us
  100.     void OnCollisionEnter(Collision other) {
  101.         //If we're player independent, just stop this D:
  102.         if(PlayerIndependent) {
  103.             return;
  104.         }
  105.         if(other.gameObject.tag=="Player") {
  106.             StartCoroutine(DoTheBlinking());
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement