DogCatchDetection.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Debug.Log("catchProbability:" + catchProbability);
  36. if (other.gameObject.tag == "Throw Material" && other.transform.position.z > -9 && catchProbability>0.6f) // >-9表示丢出一段距离后再检测
  37. {
  38. // 调整camera
  39. var CamPlayer = GameObject.Find("Player CAM").GetComponent<CinemachineVirtualCamera>();
  40. var CamDog = GameObject.Find("Dog CAM").GetComponent<CinemachineVirtualCamera>();
  41. CamPlayer.Priority = 10;
  42. CamDog.Priority = 0;
  43. Debug.Log("狗狗接到飞盘了" + other.gameObject.tag);
  44. PlayData.throwCatched = true;
  45. PlayData.throwEndPosition = other.transform.position; // 提交飞盘停止位置
  46. PlayData.gameStatus = PlayData.GameStatus.finishSuccess;
  47. Debug.Log("toy flied distance:" + PlayData.throwDistance());
  48. // 将玩具碰撞体去掉
  49. Rigidbody rb = toy.GetComponent<Rigidbody>();
  50. rb.isKinematic = true; // 去除重力影响
  51. BoxCollider boxCollider = toy.GetComponent<BoxCollider>();
  52. boxCollider.enabled = false; // 去除碰撞体
  53. // 将玩具嵌入狗的身体内
  54. var dog = UserProperty.dogs[GameData.focusDog]; // 不同类型的狗可能嵌入位置不同
  55. if (dog.breed == "shibaInu")
  56. {
  57. dogMouth = GameObject.Find("mouth");
  58. toy.transform.SetParent(dogMouth.transform, false);
  59. toy.transform.localPosition = new Vector3(0, 0.13f, 0.008f);
  60. toy.transform.localRotation = Quaternion.Euler(0, 0, 0);
  61. toy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  62. }
  63. isDogRunBack = true;
  64. }
  65. }
  66. // 抓到玩具后狗狗跑回来
  67. private void DogRunBack()
  68. {
  69. DogProperty dogProperty = UserProperty.dogs[0]; // 读取狗的数据
  70. var basePosition = new Vector3(0, 0.4f, -10.85f);
  71. float dogSpeed = dogProperty.runSpeed / 10; // 狗的移动速度
  72. dog.transform.LookAt(basePosition);
  73. //dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, Quaternion.LookRotation(basePosition), 10800 * Time.deltaTime);
  74. dog.transform.position = Vector3.MoveTowards(dog.transform.position, basePosition, dogSpeed * Time.deltaTime);
  75. if (dog.transform.position == basePosition)
  76. {
  77. isDogBack = true;
  78. }
  79. }
  80. }