DogCatchDetection.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. var dog = UserProperty.dogs[GameData.focusDog]; // 不同类型的狗可能嵌入位置不同
  58. if (dog.breed == "shibaInu")
  59. {
  60. dogMouth = GameObject.Find("mouth");
  61. toy.transform.SetParent(dogMouth.transform, false);
  62. toy.transform.localPosition = new Vector3(0, 0.13f, 0.008f);
  63. toy.transform.localRotation = Quaternion.Euler(0, 0, 0);
  64. toy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  65. }
  66. isDogRunBack = true;
  67. }
  68. }
  69. // 抓到玩具后狗狗跑回来
  70. private void DogRunBack()
  71. {
  72. DogProperty dogProperty = UserProperty.dogs[0]; // 读取狗的数据
  73. var basePosition = new Vector3(0, 0.4f, -10.85f);
  74. float dogSpeed = (100 + dogProperty.runSpeed) / 10 / 2; // 狗的移动速度
  75. dog.transform.LookAt(basePosition);
  76. //dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, Quaternion.LookRotation(basePosition), 10800 * Time.deltaTime);
  77. dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
  78. if (dog.transform.position == basePosition)
  79. {
  80. isDogBack = true;
  81. }
  82. }
  83. }