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.

98 lines
3.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Networking.Client;
  5. public class FloatingOnWater : MonoBehaviour
  6. {
  7. bool characterInWater = true;
  8. public ConnectedClients clientData;
  9. private List<string> Names;
  10. public GameObject lilypad;
  11. GameObject player;
  12. string charName = "Character";
  13. public bool triggeranimate;
  14. public bool triggeranimate1;
  15. private void Update()
  16. {
  17. //for testing purposes
  18. if (triggeranimate == true)
  19. {
  20. Animate();
  21. triggeranimate = false;
  22. }
  23. //for testing purposes
  24. if (triggeranimate1 == true)
  25. {
  26. returnToPosition();
  27. triggeranimate1 = false;
  28. }
  29. }
  30. private void Start()
  31. {
  32. for(int i = 0; i < clientData.AllClients.Count; i++)
  33. {
  34. Names.Add(clientData.AllClients[i].characterAnimal + "(Clone)");
  35. }
  36. }
  37. void OnTriggerEnter(Collider other)
  38. {
  39. for(int i = 0; i < Names.Count; i++)
  40. {
  41. if (other.gameObject.name == Names[i])
  42. {
  43. characterInWater = true;
  44. charName = other.gameObject.name;
  45. }
  46. }
  47. }
  48. IEnumerator FloatCoroutine()
  49. {
  50. float elapsedTime = 0;
  51. Vector3 startPosition = lilypad.transform.position;
  52. Vector3 endPosition = new Vector3(lilypad.transform.position.x, lilypad.transform.position.y + 1.0f, lilypad.transform.position.z);
  53. Vector3 charStartPosition = player.transform.position;
  54. Vector3 charEndPosition = new Vector3(player.transform.position.x, 1, player.transform.position.z);
  55. float time = 0.8f;
  56. while (elapsedTime < time)
  57. {
  58. lilypad.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  59. player.transform.position = Vector3.Lerp(charStartPosition, charEndPosition, (elapsedTime / time));
  60. yield return new WaitForEndOfFrame();
  61. elapsedTime += Time.deltaTime;
  62. }
  63. player.transform.position = charEndPosition;
  64. lilypad.transform.position = endPosition;
  65. }
  66. IEnumerator SinkCoroutine()
  67. {
  68. float elapsedTime = 0;
  69. Vector3 startPosition = lilypad.transform.position;
  70. Vector3 endPosition = new Vector3(lilypad.transform.position.x, lilypad.transform.position.y - 1.0f, lilypad.transform.position.z);
  71. float time = 0.8f;
  72. while (elapsedTime < time)
  73. {
  74. lilypad.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  75. yield return new WaitForEndOfFrame();
  76. elapsedTime += Time.deltaTime;
  77. }
  78. lilypad.transform.position = endPosition;
  79. }
  80. public void Animate()
  81. {
  82. if(characterInWater == true)
  83. {
  84. player = GameObject.Find(charName);
  85. StartCoroutine(FloatCoroutine());
  86. }
  87. }
  88. public void returnToPosition()
  89. {
  90. StartCoroutine(SinkCoroutine());
  91. }
  92. }