DogCatchDetection.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Cinemachine;
  2. using TMPro;
  3. using UnityEngine;
  4. /* 主要管理狗和飞盘触控管理发生后的管理行为 */
  5. public class DogCatchDetection : MonoBehaviour
  6. {
  7. GameObject toy;
  8. GameObject dog;
  9. GameObject dogMouth;
  10. bool isDogRunBack = false;
  11. bool isDogBack = false;
  12. // Start is called once before the first execution of Update after the MonoBehaviour is created
  13. void Start()
  14. {
  15. toy = GameObject.Find("toy");
  16. dog = GameObject.Find("dog");
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. if (isDogRunBack)
  22. {
  23. DogRunBack();
  24. }
  25. if (isDogBack)
  26. {
  27. Animator animator = dog.GetComponent<Animator>();
  28. animator.SetBool("runState", false);
  29. }
  30. }
  31. // 飞盘撞到狗,判断为成功
  32. private void OnTriggerEnter(Collider other)
  33. {
  34. var DogProperty = UserProperty.dogs[GameData.focusDog]; // 读取狗的数据
  35. //float catchProbability = (DogProperty.frisbeeSkill*Random.Range(0f,1f)+100)/200; // 狗的抓取概率
  36. bool catchProbability = GameTool.Random100Check(DogProperty.frisbeeSkill); // 狗的抓取概率
  37. Debug.Log("catchProbability:" + catchProbability);
  38. //if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9 && catchProbability>0.6f) // >-9表示丢出一段距离后再检测
  39. if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9 && catchProbability) // >-9表示丢出一段距离后再检测
  40. {
  41. // 调整camera
  42. var CamPlayer = GameObject.Find("Player CAM").GetComponent<CinemachineVirtualCamera>();
  43. var CamDog = GameObject.Find("Dog CAM").GetComponent<CinemachineVirtualCamera>();
  44. CamPlayer.Priority = 10;
  45. CamDog.Priority = 0;
  46. Debug.Log("狗狗接到飞盘了" + other.gameObject.tag);
  47. PlayData.throwCatched = true;
  48. PlayData.throwEndPosition = other.transform.position; // 提交飞盘停止位置
  49. PlayData.gameStatus = PlayData.GameStatus.finishSuccess;
  50. Debug.Log("toy flied distance:" + PlayData.throwDistance());
  51. // 将玩具碰撞体去掉
  52. Rigidbody rb = toy.GetComponent<Rigidbody>();
  53. rb.isKinematic = true; // 去除重力影响
  54. BoxCollider boxCollider = toy.GetComponent<BoxCollider>();
  55. boxCollider.enabled = false; // 去除碰撞体
  56. // 停止飞盘旋转
  57. FrisbeeRotateController frisbeeRotateController = toy.GetComponent<FrisbeeRotateController>();
  58. frisbeeRotateController.rotationSpeed = 0f; // 停止旋转
  59. // 将玩具嵌入狗的身体内
  60. var dog = UserProperty.dogs[GameData.focusDog]; // 不同类型的狗可能嵌入位置不同
  61. if (dog.breed == "shibaInu")
  62. {
  63. dogMouth = GameObject.Find("mouth");
  64. toy.transform.SetParent(dogMouth.transform, false);
  65. toy.transform.localPosition = new Vector3(0, 0.13f, 0.008f);
  66. toy.transform.localRotation = Quaternion.Euler(0, 0, 0);
  67. toy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  68. }
  69. isDogRunBack = true;
  70. }
  71. }
  72. // 抓到玩具后狗狗跑回来
  73. private void DogRunBack()
  74. {
  75. DogProperty dogProperty = UserProperty.dogs[0]; // 读取狗的数据
  76. var basePosition = new Vector3(0, 0.4f, -10.85f);
  77. float dogSpeed = (100 + dogProperty.runSpeed) / 10 / 2; // 狗的移动速度
  78. dog.transform.LookAt(basePosition);
  79. //dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, Quaternion.LookRotation(basePosition), 10800 * Time.deltaTime);
  80. dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
  81. if (dog.transform.position == basePosition)
  82. {
  83. isDogBack = true;
  84. }
  85. }
  86. }