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.

86 lines
2.7 KiB

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