123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using Cinemachine;
- using UnityEngine;
- /* 主要管理狗和飞盘触控管理发生后的管理行为 */
- public class DogCatchDetection : MonoBehaviour
- {
- GameObject toy;
- GameObject dog;
- GameObject dogMouth;
- bool isDogRunBack = false;
- bool isDogBack = false;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- toy = GameObject.Find("toy");
- dog = GameObject.Find("dog");
-
- }
- // Update is called once per frame
- void Update()
- {
- if (isDogRunBack)
- {
- DogRunBack();
- }
- if (isDogBack)
- {
- Animator animator = dog.GetComponent<Animator>();
- animator.SetBool("runState", false);
- }
- }
- // 飞盘撞到狗,判断为成功
- private void OnTriggerEnter(Collider other)
- {
-
- if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9) // >-9表示丢出一段距离后再检测
- {
- // 调整camera
- var CamPlayer = GameObject.Find("Player CAM").GetComponent<CinemachineVirtualCamera>();
- var CamDog = GameObject.Find("Dog CAM").GetComponent<CinemachineVirtualCamera>();
- CamPlayer.Priority = 10;
- CamDog.Priority = 0;
- Debug.Log("狗狗接到飞盘了" + other.gameObject.tag);
- PlayData.throwCatched = true;
- PlayData.throwEndPosition = other.transform.position; // 提交飞盘停止位置
- PlayData.gameStatus = PlayData.GameStatus.finishSuccess;
- Debug.Log("toy flied distance:" + PlayData.throwDistance());
- // 将玩具碰撞体去掉
- Rigidbody rb = toy.GetComponent<Rigidbody>();
- rb.isKinematic = true; // 去除重力影响
- BoxCollider boxCollider = toy.GetComponent<BoxCollider>();
- boxCollider.enabled = false; // 去除碰撞体
- // 将玩具嵌入狗的身体内
- var dog = UserProperty.dogs[GameData.focusDog]; // 不同类型的狗可能嵌入位置不同
- if (dog.breed == "shibaInu")
- {
- dogMouth = GameObject.Find("mouth");
- toy.transform.SetParent(dogMouth.transform, false);
- toy.transform.localPosition = new Vector3(0, 0.13f, 0);
- toy.transform.localRotation = Quaternion.Euler(0, 0, 0);
- toy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
- }
-
- isDogRunBack = true;
- }
- }
- // 抓到玩具后狗狗跑回来
- private void DogRunBack()
- {
- DogProperty dogProperty = UserProperty.dogs[0]; // 读取狗的数据
- var basePosition = new Vector3(0, 0.4f, -10.85f);
- float dogSpeed = dogProperty.runSpeed / 10; // 狗的移动速度
- dog.transform.LookAt(basePosition);
- //dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, Quaternion.LookRotation(basePosition), 10800 * Time.deltaTime);
- dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
- if (dog.transform.position == basePosition)
- {
- isDogBack = true;
- }
- }
- }
|