1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using TMPro;
- 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");
- dogMouth = GameObject.Find("mouth");
- }
- // 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表示丢出一段距离后再检测
- {
- 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; // 去除碰撞体
- toy.transform.SetParent(dogMouth.transform, false);
- toy.transform.localPosition = new Vector3(0, 0.1f, 0);
- toy.transform.localRotation = Quaternion.Euler(90,0, 0);
- toy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
- isDogRunBack = true;
- }
- }
- // 狗狗跑回来
- private void DogRunBack()
- {
- DogProperty dogProperty = EnviromentSetting.puppies[0]; // 读取狗的数据
- var basePosition = new Vector3(0, 0.4f, -10.85f);
- float dogSpeed = dogProperty.runSpeed / 10; // 狗的移动速度
- dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, Quaternion.LookRotation(basePosition), 1080 * Time.deltaTime);
- dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
- if (dog.transform.position == basePosition)
- {
- isDogBack = true;
- }
- }
- }
|