DogCatchDetection.cs 3.8 KB

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