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.

66 lines
1.9 KiB

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