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.

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