You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CubeWithCrystals : MonoBehaviour
  5. {
  6. public GameObject crystals;
  7. public bool triggeranimate;
  8. public bool triggeranimate1;
  9. private void Update()
  10. {
  11. //for testing purposes
  12. if (triggeranimate == true)
  13. {
  14. Animate();
  15. triggeranimate = false;
  16. }
  17. if (triggeranimate1 == true)
  18. {
  19. returnToPosition();
  20. triggeranimate1 = false;
  21. }
  22. }
  23. IEnumerator GrowCoroutine()
  24. {
  25. float elapsedTime = 0;
  26. Vector3 startPosition = crystals.transform.position;
  27. Vector3 endPosition = new Vector3(crystals.transform.position.x, crystals.transform.position.y + 1.0f, crystals.transform.position.z);
  28. float time = 0.8f;
  29. while (elapsedTime < time)
  30. {
  31. transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  32. yield return new WaitForEndOfFrame();
  33. elapsedTime += Time.deltaTime;
  34. }
  35. crystals.transform.position = endPosition;
  36. }
  37. IEnumerator ShrinkCoroutine()
  38. {
  39. float elapsedTime = 0;
  40. Vector3 startPosition = crystals.transform.position;
  41. Vector3 endPosition = new Vector3(crystals.transform.position.x, crystals.transform.position.y - 1.0f, crystals.transform.position.z);
  42. float time = 0.8f;
  43. while (elapsedTime < time)
  44. {
  45. transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  46. yield return new WaitForEndOfFrame();
  47. elapsedTime += Time.deltaTime;
  48. }
  49. crystals.transform.position = endPosition;
  50. }
  51. public void Animate()
  52. {
  53. StartCoroutine(GrowCoroutine());
  54. }
  55. public void returnToPosition()
  56. {
  57. StartCoroutine(ShrinkCoroutine());
  58. }
  59. }