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.

96 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. if (triggeranimate1 == true)
  24. {
  25. returnToPosition();
  26. triggeranimate1 = false;
  27. }
  28. }
  29. private void Start()
  30. {
  31. for(int i = 0; i < clientData.AllClients.Count; i++)
  32. {
  33. Names.Add(clientData.AllClients[i].characterAnimal + "(Clone)");
  34. }
  35. }
  36. void OnTriggerEnter(Collider other)
  37. {
  38. for(int i = 0; i < Names.Count; i++)
  39. {
  40. if (other.gameObject.name == Names[i])
  41. {
  42. characterInWater = true;
  43. charName = other.gameObject.name;
  44. }
  45. }
  46. }
  47. IEnumerator FloatCoroutine()
  48. {
  49. float elapsedTime = 0;
  50. Vector3 startPosition = lilypad.transform.position;
  51. Vector3 endPosition = new Vector3(lilypad.transform.position.x, lilypad.transform.position.y + 1.0f, lilypad.transform.position.z);
  52. Vector3 charStartPosition = player.transform.position;
  53. Vector3 charEndPosition = new Vector3(player.transform.position.x, 1, player.transform.position.z);
  54. float time = 0.8f;
  55. while (elapsedTime < time)
  56. {
  57. lilypad.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  58. player.transform.position = Vector3.Lerp(charStartPosition, charEndPosition, (elapsedTime / time));
  59. yield return new WaitForEndOfFrame();
  60. elapsedTime += Time.deltaTime;
  61. }
  62. player.transform.position = charEndPosition;
  63. lilypad.transform.position = endPosition;
  64. }
  65. IEnumerator SinkCoroutine()
  66. {
  67. float elapsedTime = 0;
  68. Vector3 startPosition = lilypad.transform.position;
  69. Vector3 endPosition = new Vector3(lilypad.transform.position.x, lilypad.transform.position.y - 1.0f, lilypad.transform.position.z);
  70. float time = 0.8f;
  71. while (elapsedTime < time)
  72. {
  73. lilypad.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  74. yield return new WaitForEndOfFrame();
  75. elapsedTime += Time.deltaTime;
  76. }
  77. lilypad.transform.position = endPosition;
  78. }
  79. public void Animate()
  80. {
  81. if(characterInWater == true)
  82. {
  83. player = GameObject.Find(charName);
  84. StartCoroutine(FloatCoroutine());
  85. }
  86. }
  87. public void returnToPosition()
  88. {
  89. StartCoroutine(SinkCoroutine());
  90. }
  91. }