AdjustZAccordingToScale.cs 807 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is used by the background loading sperms and is responsible for putting
  5. /// them further away from the camera according the scale, to give proper depth
  6. /// </summary>
  7. public class AdjustZAccordingToScale : MonoBehaviour
  8. {
  9. public float nearZ = 1;
  10. public float farZ = 10;
  11. bool _isSetup;
  12. void OnEnable()
  13. {
  14. //have to delay setup on update due to pooling
  15. _isSetup = false;
  16. }
  17. void Update()
  18. {
  19. if (!_isSetup)
  20. {
  21. float percentageScaled = Mathf.Lerp(LoadingBackgroundSpermsManager.MAX_SCALE, LoadingBackgroundSpermsManager.MIN_SCALE, transform.localScale.y);
  22. Vector3 pos = transform.localPosition;
  23. pos.z = Mathf.Lerp(nearZ, farZ, percentageScaled);
  24. transform.localPosition = pos;
  25. _isSetup = true;
  26. }
  27. }
  28. }