using System; using UnityEngine; /* Home场景中碗碰撞体检测和控制 * 当狗开始喝水和吃饭动作后,狗向碗移动,检测碰撞后停止移动开始喝水吃饭动作 * 场景中有多个狗,每次检测碰撞后反向遍历dogInScene,逐个控制移动和喝水 */ public class BowlColliderController : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created private GameObject bowls, bowlFood, bowlWater; private GameObject thisObject; void OnEnable() { bowls = GameObject.Find("Bowls"); thisObject = this.gameObject; bowlFood = bowls.transform.Find("Bowl_food").gameObject; bowlWater = bowls.transform.Find("Bowl_water").gameObject; } // Update is called once per frame //void Update() //{ //} private void OnTriggerEnter(Collider other) { foreach (var dogInScene in HomeController.dogsInScene) { if (dogInScene.gameObject == other.gameObject) { // 检测哪一只doginScene发生碰撞 if (thisObject.name == "Bowl_water") { dogInScene.isMovingToBowl = false; dogInScene.drinkStartTime = DateTime.Now; dogInScene.animator.SetTrigger("drink"); dogInScene.animator.SetBool("isDrinking", true); StartCoroutine(dogInScene.DrinkAnimation()); //dogInScene.DrinkAnimation(); }else if (thisObject.name == "Bowl_food") { dogInScene.eatStartTime = DateTime.Now; dogInScene.Eat(); } } } } }