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.

65 lines
1.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Water : ActiveBlock
  5. {
  6. [SerializeField]
  7. [Tooltip("GameObject which appears when character respawns")]
  8. private GameObject lillyPad;
  9. [SerializeField]
  10. [Tooltip("Distance character will be submerged into the water")]
  11. private float FallDistance = 0.5f;
  12. private Character trappedCharacter;
  13. public override void OnWalkedOnByPlayer(PlayerData player)
  14. {
  15. base.OnWalkedOnByPlayer(player);
  16. if (trappedCharacter == null)
  17. {
  18. player.character = trappedCharacter;
  19. trappedCharacter.stuck = true;
  20. trappedCharacter.StartAnimation(Character.Animation.Hit);
  21. StartCoroutine(LerpToPosition(trappedCharacter.transform, VisualPosition + Vector3.down * FallDistance, 1));
  22. }
  23. }
  24. public override void OnRoundEnd(PlayerData[] allPlayers)
  25. {
  26. base.OnRoundEnd(allPlayers);
  27. if (trappedCharacter != null)
  28. {
  29. StartCoroutine(LerpToPosition(trappedCharacter.transform, VisualPosition, 1));
  30. StartCoroutine(LerpToPosition(lillyPad.transform, VisualPosition, 1));
  31. trappedCharacter.stuck = false;
  32. }
  33. }
  34. private IEnumerator LerpToPosition(Transform target, Vector3 endPos, float time)
  35. {
  36. Vector3 _startPos = target.position;
  37. float elapsedTime = 0;
  38. while (elapsedTime / time < 1)
  39. {
  40. target.position = Vector3.Lerp(_startPos, endPos, (elapsedTime / time));
  41. yield return new WaitForEndOfFrame();
  42. elapsedTime += Time.deltaTime;
  43. }
  44. target.position = endPos;
  45. }
  46. }