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.

64 lines
1.9 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 = false;
  8. public ConnectedClients clientData;
  9. private List<string> Names;
  10. public GameObject lilypad;
  11. GameObject player;
  12. string charName;
  13. private void Start()
  14. {
  15. for(int i = 0; i < clientData.AllClients.Count; i++)
  16. {
  17. Names.Add(clientData.AllClients[i].characterAnimal + "(Clone)");
  18. }
  19. }
  20. void OnTriggerEnter(Collider other)
  21. {
  22. for(int i = 0; i < Names.Count; i++)
  23. {
  24. if (other.gameObject.name == Names[i])
  25. {
  26. characterInWater = true;
  27. charName = other.gameObject.name;
  28. }
  29. }
  30. }
  31. IEnumerator FloatSinkCoroutine(Vector3 endPosition)
  32. {
  33. float elapsedTime = 0;
  34. Vector3 startPosition = lilypad.transform.position;
  35. Vector3 charStartPosition = player.transform.position;
  36. Vector3 charEndPosition = new Vector3(player.transform.position.x, 1, player.transform.position.z);
  37. float time = 0.8f;
  38. while (elapsedTime < time)
  39. {
  40. lilypad.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  41. player.transform.position = Vector3.Lerp(charStartPosition, charEndPosition, (elapsedTime / time));
  42. yield return new WaitForEndOfFrame();
  43. elapsedTime += Time.deltaTime;
  44. }
  45. player.transform.position = charEndPosition;
  46. lilypad.transform.position = endPosition;
  47. }
  48. public void Animate()
  49. {
  50. if(characterInWater == true)
  51. {
  52. player = GameObject.Find(charName);
  53. StartCoroutine(FloatSinkCoroutine(new Vector3(0, 1, 0)));
  54. }
  55. }
  56. public void returnToPosition()
  57. {
  58. StartCoroutine(FloatSinkCoroutine(new Vector3(0, 0, 0)));
  59. }
  60. }