Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Elevator : SimpleStateMachine {
  5.  
  6.     public bool Bottom;
  7.  
  8.     public float Rise = 6.0f;
  9.     public float Horizontal = 3.0f;
  10.     public float BoardingHeight = 1.0f;
  11.  
  12.     public float RiseSpeed = 1.0f;
  13.     public float SwapSpeed = 2.0f;
  14.  
  15.     private enum ElevatorStates { Rotate, Descend, Ascend, Swap }
  16.  
  17.     private Vector3 ascendRoot;
  18.     private Vector3 ascendTarget;
  19.     private Vector3 flipTarget;
  20.     private Vector3 descendRoot;
  21.  
  22.     private Vector3 flipOrigin;
  23.  
  24.     private Vector3 forward;
  25.  
  26.     void Start () {        
  27.  
  28.         forward = transform.forward;
  29.  
  30.         ascendRoot = transform.position;
  31.         ascendTarget = ascendRoot + Vector3.up * Rise;
  32.         flipTarget = ascendTarget + forward * Horizontal;
  33.         descendRoot = flipTarget - Vector3.up * Rise;
  34.  
  35.         flipOrigin = ascendTarget + forward * Horizontal * 0.5f;
  36.  
  37.         if (Bottom)
  38.         {
  39.             currentState = ElevatorStates.Ascend;
  40.         }
  41.         else
  42.         {
  43.             currentState = ElevatorStates.Descend;
  44.             transform.position = flipTarget;
  45.         }
  46.     }  
  47.  
  48.     void Ascend_FixedUpdate () {
  49.  
  50.         if (transform.position == ascendTarget)
  51.         {
  52.             currentState = ElevatorStates.Rotate;
  53.             return;
  54.         }
  55.  
  56.         transform.position = Vector3.MoveTowards(transform.position, ascendTarget, RiseSpeed * Time.deltaTime);
  57.     }
  58.  
  59.     float rot;
  60.  
  61.     void Rotate_EnterState()
  62.     {
  63.         rot = 0;
  64.     }
  65.  
  66.     void Rotate_FixedUpdate()
  67.     {
  68.         rot += SwapSpeed * Time.deltaTime;
  69.  
  70.         transform.RotateAround(flipOrigin, transform.right, SwapSpeed * Time.deltaTime);
  71.  
  72.         if (rot > 180)
  73.         {
  74.             transform.position = flipTarget;
  75.             transform.rotation = Quaternion.identity;
  76.             currentState = ElevatorStates.Descend;
  77.             return;
  78.         }
  79.     }
  80.  
  81.     void Descend_FixedUpdate()
  82.     {
  83.  
  84.         if (transform.position == descendRoot)
  85.         {
  86.             currentState = ElevatorStates.Swap;
  87.             return;
  88.         }
  89.  
  90.         transform.position = Vector3.MoveTowards(transform.position, descendRoot, RiseSpeed * Time.deltaTime);
  91.     }
  92.  
  93.     void Swap_FixedUpdate()
  94.     {
  95.         float time = 180 / SwapSpeed;
  96.         float distance = Vector3.Distance(descendRoot, ascendRoot);
  97.  
  98.         float velocity = distance / time;
  99.  
  100.         transform.position -= forward * velocity * Time.deltaTime;
  101.  
  102.         if (Vector3.Distance(descendRoot, transform.position) > Horizontal)
  103.         {
  104.             transform.position = ascendRoot;
  105.             currentState = ElevatorStates.Ascend;
  106.             return;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement