123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /* 处理Home场景中狗的碰撞检测
- * 该脚本用于处理狗与其他物体的碰撞检测
- * 通过OnTriggerEnter方法来检测与其他物体的碰撞
- * 该脚本需要挂载在狗的GameObject上
- */
- public class DogCollisionController : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- // void Start()
- // {
- // }
- // // Update is called once per frame
- // void Update()
- // {
- // }
- void OnTriggerEnter(Collider other)
- {
- BoxCollider boxCollider = this.GetComponent<BoxCollider>();
- if (boxCollider.isTrigger == true)
- {
- Debug.Log("collision this" + this.transform.position);
- Debug.Log("collision other" + other.transform.position);
- Vector3 direction = transform.position - other.transform.position;
- Vector3 moveToLocation = this.transform.position + direction.normalized * 0.5f;
- if (moveToLocation.x < -5f){moveToLocation.x = -5f;}
- if (moveToLocation.x > 5f){moveToLocation.x = 5f;}
- if (moveToLocation.z > 5f){moveToLocation.z = 5f;}
- Debug.Log("collision moveToLocation" + moveToLocation);
- GameObject thisDog = this.gameObject;
- foreach (var dog in HomeController.dogsInScene)
- {
- if (dog.gameObject == thisDog && dog.dogState != DogState.SLEEP)
- {
- // 如果狗正在睡觉,则不处理碰撞
- // 只处理非睡觉状态下的碰撞
- Debug.Log("dog move to location" + moveToLocation);
- {
- dog.moveToLocation = moveToLocation;
- dog.RandomMove();
- break;
- }
- }
- }
- }
- }
|