Advertisement
BinaryImpactG

Unity Tip - Dancing Fire

Jan 18th, 2022
1,029
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// This class moves an object, usually a light, in a random pattern to
  5. /// simulate the movement of flames inside a fire and make the lighting and
  6. /// shadowing more natural.
  7. /// </summary>
  8. public class FireLightMovement : MonoBehaviour {
  9.     [Tooltip ( "The maximum amplitude in each principal direction." )]
  10.     public Vector3 amplitude = Vector3.one * 0.1f;
  11.     [Tooltip ( "The base frequency for the movement." )]
  12.     public float frequency = 0.1f;
  13.     [Tooltip ( "The deviation that is allowed for each movement. Ranges from 0 to 0.95 of the frequency." )]
  14.     [Range ( 0f, 0.95f )]
  15.     public float frequencyDeviation = 0.5f;
  16.  
  17.     private Vector3 _initialPosition = Vector3.zero;            // Stores the initial position of the object.
  18.     private Vector3 _randomOffset = Vector3.one;                // The randomized offset from the position.
  19.     private Vector3 _targetPosition = Vector3.zero;             // The current target position.
  20.     private Vector3 _lastPosition = Vector3.zero;               // The last taret position.
  21.     private float _cooldown = 0f;
  22.     private float _frequencyDeviationOffset = 0f;
  23.     private float _travel = 0f;
  24.  
  25.     void Start() {
  26.         // We need to store the initial position to not wander off.
  27.         _initialPosition = _lastPosition = _targetPosition = transform.position;
  28.     }
  29.  
  30.     void Update() {
  31.         // The calculations for a new offset are only done when the cooldown is over.
  32.         if ( _cooldown <= 0f ) {
  33.             // The offset is a random value inside a unit sphere scaled by the amplitude.
  34.             _randomOffset = Random.insideUnitSphere;
  35.             _randomOffset.Scale ( amplitude );
  36.             // The target position is calculated from the offset and the initial position.
  37.             _targetPosition = _initialPosition + _randomOffset;
  38.             // Stored for reference later.
  39.             _lastPosition = transform.position;
  40.             // The frequency deviation is calculated to make the movement more random.
  41.             _frequencyDeviationOffset = Random.Range ( -1.0f, 1.0f ) * ( frequencyDeviation * frequency );
  42.             // The cooldown needs to respect the calculated offset.
  43.             _cooldown = frequency + _frequencyDeviationOffset;
  44.         }
  45.  
  46.         // The travel describes which percentage of the cooldown is already over.
  47.         if ( _cooldown < Time.deltaTime ) {
  48.             // If the cooldown is shorter than one frame it is set to zero.
  49.             _travel = 0f;
  50.         } else {
  51.             _travel = _cooldown / ( frequency + _frequencyDeviationOffset );
  52.         }
  53.  
  54.         /*
  55.          * The object is moved from the last position to the new target position by
  56.          * a percentage of the distance that is proportional to the remaining cooldown.
  57.          */
  58.         transform.position = _lastPosition + ( _targetPosition - _lastPosition ) * Mathf.Clamp01 ( 1f - _travel );
  59.         _cooldown -= Time.deltaTime;
  60.  
  61.     }
  62. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement