123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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 = UserProperty.dogs[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), 10800 * Time.deltaTime);
- dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
- if (dog.transform.position == basePosition)
- {
- isDogBack = true;
- }
- }
- }
|