1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using Cinemachine;
- 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");
- }
- // 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)
- {
- var DogProperty = UserProperty.dogs[GameData.focusDog]; // 读取狗的数据
- //float catchProbability = (DogProperty.frisbeeSkill*Random.Range(0f,1f)+100)/200; // 狗的抓取概率
- bool catchProbability = GameTool.Random100Check(DogProperty.frisbeeSkill); // 狗的抓取概率
- Debug.Log("catchProbability:" + catchProbability);
- //if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9 && catchProbability>0.6f) // >-9表示丢出一段距离后再检测
- if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9 && catchProbability) // >-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.008f);
- 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 = (100 + dogProperty.runSpeed) / 10 / 2; // 狗的移动速度
- 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;
- }
- }
- }
|