DogCatchDetection.cs 3.0 KB

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