HorizontalMovementAccordingToScale.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. /// <summary>
  5. /// This component is very similar to the HorizontalMovement but is used the loading background
  6. /// sperms. The velocity is scaled down according to their scale to give the illusion of depth
  7. /// </summary>T
  8. public class HorizontalMovementAccordingToScale : MovementBase
  9. {
  10. public float minSpeed = 300;
  11. public float maxSpeed = 600;
  12. float _speed = 300;
  13. bool _isSetup;
  14. void OnEnable()
  15. {
  16. //have to delay setup on update due to pooling
  17. _isSetup = false;
  18. }
  19. void Update()
  20. {
  21. if (!_isSetup)
  22. {
  23. _speed = RandomSingleton.Instance.NextFloat(minSpeed, maxSpeed);
  24. _speed *= transform.localScale.y; //use the scaling as a factor of speed. The more scaled down == further away == slower
  25. _isSetup = true;
  26. }
  27. float delta = _speed * Time.deltaTime;
  28. transform.localPosition += new Vector3(delta, 0);
  29. }
  30. public override void Encode(List<byte> data)
  31. {
  32. data.AddRange(BitConverter.GetBytes(minSpeed));
  33. data.AddRange(BitConverter.GetBytes(maxSpeed));
  34. }
  35. public override void Decode(byte[] data, ref int index)
  36. {
  37. minSpeed = BitConverter.ToSingle(data, index);
  38. index += sizeof(float);
  39. maxSpeed = BitConverter.ToSingle(data, index);
  40. index += sizeof(float);
  41. }
  42. }