DogCatchDetection.cs 3.2 KB

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