1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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.moveToLocation = moveToLocation;
- dog.RandomMove();
- break;
- }
- }
- }
- }
- }
|