SHARE
TWEET

Object Transitioner

a guest Sep 18th, 2015 222 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public struct Transitionable
  2. {
  3.     public Transform trans           { get; private set; } // The transform of the transitionable
  4.     public Collider  collider        { get; private set; } // The collider of the transitionable
  5.     public Float3    oPos            { get; private set; } // The start position of the transitionable
  6.     public Float3    ePos            { get;         set; } // The end   position of the transitionable
  7.     public Float3    oScale          { get;         set; } // The start scale    of the transitionable
  8.     public Float3    eScale          { get;         set; } // The end   scale    of the transitionable
  9.     public bool      enabledCollider { get;         set; } // The state of the collider
  10.  
  11.  
  12.     // Get/Sets the position of the transform
  13.     public Vector3 position
  14.     {
  15.         get { return trans.position ; }
  16.         set { trans.position = value; }
  17.     }
  18.  
  19.     // Get/Sets the scale of the transform
  20.     public Vector3 scale
  21.     {
  22.         get { return trans.localScale ; }
  23.         set { trans.localScale = value; }
  24.     }
  25.  
  26.     // Constrcuter
  27.     public Transitionable( Transform _core )
  28.     {
  29.         trans    = _core                         ; // Store the Transitionable's transform
  30.         collider = _core.GetComponent<Collider>(); // Store the collider for culling
  31.  
  32.         enabledCollider = collider.enabled;
  33.  
  34.         oPos   = new Float3( _core.position.x  , _core.position.y  , _core.position.z   ); // Assign the starting position of the Transitionable
  35.         oScale = new Float3( _core.localScale.x, _core.localScale.y, _core.localScale.z ); // Assign the starting scale of the Transitionable
  36.     }
  37. }
  38.  
  39.  
  40. // We use a custom struct instead of unity 3D's vector class
  41. // As it contains methods which we will never need for this
  42. // it's pretty self explanitory ( it's a struct with 3 floats ).
  43.  
  44. public struct Float3
  45. {
  46.     public float x { get; set; }
  47.     public float y { get; set; }
  48.     public float z { get; set; }
  49.  
  50.     public Float3(float _x, float _y, float _z)
  51.     {
  52.         x = _x;
  53.         y = _y;
  54.         z = _z;
  55.     }
  56. }
  57.  
  58. public class ObjectTransitioner : MonoBehaviour
  59. {
  60.         [SerializeField] private Transform  _relationalObject   ; // The object which everything moves in relation to
  61.         [SerializeField] private string[]   _transitionableTags ; // The tags of which the objects are labled to gather them specific object/s
  62.         [SerializeField] private float      _minCheckDist = 3F  ; // The min distance that is checked
  63.         [SerializeField] private float      _midCheckDist = 15F ; // The mid distance that is checked
  64.         [SerializeField] private float      _maxCheckDist = 40F ; // The max distance that is checked
  65.         [SerializeField] private float      _mSpeed       = 10F ; // The speed of the transition
  66.         [SerializeField] private float      _minRandPos   = -40F; // The min random position of the transitionables
  67.         [SerializeField] private float      _maxRandPos   =  40F; // The max random position of the transitionables
  68.         [SerializeField] private float      _minRandScale = 1.0F; // The min random scale    of the transitionables
  69.         [SerializeField] private float      _maxRandScale = 0.2F; // The max random scale    of the transitionables
  70.          
  71.         private Transitionable[] _toMove;                         // All of the transitionables ( change to list to edit in runtime )
  72.  
  73.         // Called when loaded
  74.         private void Awake()
  75.         {
  76.                 FindAllTransitioners(); // Find all the transitionabls
  77.         }
  78.  
  79.         // We could enbed this into a custom editor for efficiency ( which would be a better option for a propper game ),
  80.         // however to make this example easier to follow through we simply do it when the component is started. Basically,
  81.         // This method is a complete performance mess, so apart from testing just dont use it.
  82.         private void FindAllTransitioners()
  83.         {
  84.                 List<Transitionable> _foundObjects = new List<Transitionable>(); // All the objects that are found
  85.  
  86.                 for( int _i = 0; _i < _transitionableTags.Length; _i++ ) // Itterate through the tags
  87.                 {
  88.                         GameObject[] _cFound = GameObject.FindGameObjectsWithTag( _transitionableTags[_i] ); // Find the current objects which have the itterated tag
  89.  
  90.                         for( int _iFound = 0; _iFound < _cFound.Length; _iFound++ ) // Loop through all the found objects
  91.                         {
  92.                                 Transitionable _t = new Transitionable( _cFound[_iFound].transform ); // Create a new Transitionable with the found transform
  93.  
  94.                                 // Create a random end position for the object
  95.                                 _t.ePos = new Float3( Random.Range(_minRandPos + _t.position.x, _maxRandPos + _t.position.x ),
  96.                                                       Random.Range(_minRandPos + _t.position.y, _maxRandPos + _t.position.y ),
  97.                                                       Random.Range(_minRandPos + _t.position.z, _maxRandPos + _t.position.z ) );
  98.  
  99.                                 // Get a random size and assign the end scale to the random size
  100.                                 float _rSize = Random.Range( _minRandScale, _maxRandScale );
  101.                                 _t.eScale = new Float3( _rSize, _rSize, _rSize );
  102.  
  103.                                 _foundObjects.Add(_t); // Add the object to the found objects list
  104.                         }
  105.                 }
  106.  
  107.                 _toMove = _foundObjects.ToArray(); // Transform the found objects list into an array
  108.         }
  109.  
  110.         private void FixedUpdate() // Update at fixed interval
  111.         {
  112.                 UpdateTransitioners(); // Update the transitioners
  113.         }
  114.        
  115.         // Identical to Vector.Distance apart from it inputs a Float3 instead of a Vector3
  116.         private float Distance( Float3 _a, Vector3 _b )
  117.         {
  118.                 Float3 _delta = new Float3(_a.x - _b.x, _a.y - _b.y, _a.z - _b.z); // Calculate the delta value
  119.  
  120.                 return Mathf.Sqrt( _delta.x * _delta.x + _delta.y * _delta.y + _delta.z * _delta.z ); // Calculate the distance
  121.         }
  122.  
  123.         private Vector3 Lerp( Vector3 _from, Float3 _to, float _t )
  124.         {
  125.                 _t = Mathf.Clamp01(_t); // Clamp the time interval
  126.  
  127.                 return new Vector3( _from.x + ( _to.x - _from.x ) * _t, _from.y + ( _to.y - _from.y ) * _t, _from.z + ( _to.z - _from.z ) * _t ); // Calculate the lerptation value
  128.         }
  129.  
  130.  
  131.         // Updates the transitioners position and size relative to the distance
  132.         private void UpdateTransitioners()
  133.         {
  134.                 for( int _i = 0; _i < _toMove.Length; _i++ ) // Itterate through the transitionables
  135.                 {
  136.                         float _d = Distance( _toMove[ _i ].oPos, _relationalObject.position ); // Calculate the distance between the relational object and current transitionable
  137.  
  138.                         if( _d < _minCheckDist ) // If the distance is less than the min distance checked
  139.                                 OnMinDist(_i, _d );  // Call respective method
  140.                         else
  141.                         if( _d < _midCheckDist ) // If the distance is less than the mid distance checked
  142.                                 OnMidDist(_i);       // Call respective method
  143.                         else
  144.                         if( _d < _maxCheckDist ) // If the distance is less than the max distance checked
  145.                                 OnMaxDist(_i);       // Call respective method
  146.                         else  
  147.                                 OnOutOfBounds(_i); // Otherwise we are out of bounds
  148.                 }
  149.         }
  150.  
  151.         private void OnMinDist( int _i, float _d)
  152.         {
  153.                 // If the collider is disabled then enable it
  154.                 if (!_toMove [_i].enabledCollider)
  155.                 {
  156.                         _toMove [_i].collider.enabled = true;
  157.                         _toMove [_i].enabledCollider  = true;
  158.                 }
  159.  
  160.                 // If distance is less than 3
  161.                 if( _d < 3.0F )
  162.                 {
  163.                         // If the distance is less than two
  164.                         if( _d < 2.0F )
  165.                         {
  166.                                 // Then just set the size and sclae instead of lerping as it is too close
  167.                                 _toMove[_i].position = new Vector3( _toMove[_i].oPos.x  , _toMove[_i].oPos.y  , _toMove[_i].oPos.z   );
  168.                                 _toMove[_i].scale    = new Vector3( _toMove[_i].oScale.x, _toMove[_i].oScale.y, _toMove[_i].oScale.z );
  169.                         }
  170.                         else
  171.                         {
  172.                                 // Lerp at a faster pace compared to normal
  173.                                 _toMove[_i].position = Lerp( _toMove[_i].position, _toMove[_i].oPos, Time.fixedDeltaTime   * ( _mSpeed * 3 ) );
  174.                                 _toMove[_i].scale    = Lerp( _toMove[_i].scale   , _toMove[_i].oScale, Time.fixedDeltaTime * ( _mSpeed     ) );
  175.                         }
  176.                 }
  177.                 else
  178.                 {
  179.                         // Lerp at a faster pace as we aer closer
  180.                         _toMove[_i].position = Lerp( _toMove[_i].position, _toMove[_i].oPos  , Time.fixedDeltaTime *   _mSpeed       );
  181.                         _toMove[_i].scale    = Lerp( _toMove[_i].scale   , _toMove[_i].oScale, Time.fixedDeltaTime * ( _mSpeed / 2)  );
  182.                 }
  183.         }
  184.  
  185.         private void OnMidDist( int _i )
  186.         {
  187.                 //Disable collision
  188.                 DisableCollision(_i);
  189.  
  190.                 //Lerp the scale and position to there original positions
  191.                 _toMove[_i].position = Lerp( _toMove[_i].position, _toMove[_i].oPos  , Time.fixedDeltaTime *   _mSpeed      );
  192.                 _toMove[_i].scale    = Lerp( _toMove[_i].scale   , _toMove[_i].oScale, Time.fixedDeltaTime * ( _mSpeed / 2) );
  193.         }
  194.  
  195.         private void OnMaxDist( int _i )
  196.         {
  197.                 // Disable collision
  198.                 DisableCollision(_i);
  199.  
  200.                 // Lerp the scale and position to there end positions
  201.                 _toMove[_i].position = Lerp( _toMove[_i].position, _toMove[_i].ePos  , Time.fixedDeltaTime *   _mSpeed      );
  202.                 _toMove[_i].scale    = Lerp( _toMove[_i].scale   , _toMove[_i].eScale, Time.fixedDeltaTime * ( _mSpeed / 2) );
  203.         }
  204.  
  205.         private void OnOutOfBounds(int _i)
  206.         {
  207.                 // Here we could also stop the rendering of the object, but for this example we'll just disable collision
  208.                 DisableCollision(_i);
  209.         }
  210.  
  211.         // Called instead of copying and pasting code, but all it does is disable collision if it's enabled
  212.         private void DisableCollision(int _i)
  213.         {
  214.                 if (_toMove [_i].enabledCollider)
  215.                 {
  216.                         _toMove [_i].collider.enabled = false;
  217.                         _toMove [_i].enabledCollider  = false;
  218.                 }
  219.         }
  220. }
RAW Paste Data
Top