BowlColliderController.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using UnityEngine;
  3. /* Home场景中碗碰撞体检测和控制
  4. * 当狗开始喝水和吃饭动作后,狗向碗移动,检测碰撞后停止移动开始喝水吃饭动作
  5. * 场景中有多个狗,每次检测碰撞后反向遍历dogInScene,逐个控制移动和喝水
  6. */
  7. public class BowlColliderController : MonoBehaviour
  8. {
  9. // Start is called once before the first execution of Update after the MonoBehaviour is created
  10. private GameObject bowls, bowlFood, bowlWater;
  11. private GameObject thisObject;
  12. void OnEnable()
  13. {
  14. bowls = GameObject.Find("Bowls");
  15. thisObject = this.gameObject;
  16. bowlFood = bowls.transform.Find("Bowl_food").gameObject;
  17. bowlWater = bowls.transform.Find("Bowl_water").gameObject;
  18. }
  19. // Update is called once per frame
  20. //void Update()
  21. //{
  22. //}
  23. private void OnTriggerEnter(Collider other)
  24. {
  25. foreach (var dogInScene in HomeController.dogsInScene)
  26. {
  27. if (dogInScene.gameObject == other.gameObject) { // 检测哪一只doginScene发生碰撞
  28. if (thisObject.name == "Bowl_water")
  29. {
  30. dogInScene.isMovingToBowl = false;
  31. dogInScene.drinkStartTime = DateTime.Now;
  32. dogInScene.animator.SetTrigger("drink");
  33. dogInScene.animator.SetBool("isDrinking", true);
  34. StartCoroutine(dogInScene.DrinkAnimation());
  35. //dogInScene.DrinkAnimation();
  36. }else if (thisObject.name == "Bowl_food")
  37. {
  38. dogInScene.eatStartTime = DateTime.Now;
  39. dogInScene.Eat();
  40. }
  41. }
  42. }
  43. }
  44. }