ArrowSync.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ArrowSync : MonoBehaviour
  5. {
  6. void Awake()
  7. {
  8. this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  9. }
  10. void Update()
  11. {
  12. transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * 15);
  13. Head().position = transform.position;
  14. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 15);
  15. }
  16. public Transform Head() {
  17. return transform.Find("Head");
  18. }
  19. Quaternion rotation;
  20. Vector3 position;
  21. bool hasPlayHitAudio = false;
  22. [System.NonSerialized] public bool isHit;
  23. [System.NonSerialized] public bool canUseSideCamera;
  24. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  25. public void SetSyncData(SyncData syncData, bool apply = false) {
  26. rotation.x = syncData.rx;
  27. rotation.y = syncData.ry;
  28. rotation.z = syncData.rz;
  29. rotation.w = syncData.rw;
  30. position.x = syncData.px;
  31. position.y = syncData.py;
  32. position.z = syncData.pz;
  33. canUseSideCamera = syncData.cs;
  34. isHit = syncData.ih;
  35. if (apply) {
  36. transform.position = position;
  37. transform.rotation = rotation;
  38. //激活镜头
  39. Transform cameraTF = this.transform.Find("Camera");
  40. cameraTF.gameObject.SetActive(true);
  41. ArrowCamera arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  42. arrowCameraComp.SetArrowSync(this);
  43. //射出的声音
  44. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  45. }
  46. if (isHit && !hasPlayHitAudio) {
  47. hasPlayHitAudio = true;
  48. int hitType = syncData.ht;
  49. if (hitType == 1 || hitType == 2) {
  50. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  51. AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  52. } else if (hitType == 4 || hitType == 5) {
  53. AudioMgr.ins.PlayArrowEnter();
  54. } else if (hitType == 3) {
  55. AudioMgr.ins.PlayCheer(false);
  56. }
  57. }
  58. }
  59. public class SyncData {
  60. [System.NonSerialized] public bool inited;
  61. static int autoID;
  62. public int id;
  63. public float rx;
  64. public float ry;
  65. public float rz;
  66. public float rw;
  67. public float px;
  68. public float py;
  69. public float pz;
  70. public bool cs;
  71. public bool ih;
  72. public int ht;
  73. public void SetData(Arrow arrow) {
  74. Quaternion r = arrow.transform.rotation;
  75. Vector3 p = arrow.Head().transform.position;
  76. rx = r.x;
  77. ry = r.y;
  78. rz = r.z;
  79. rw = r.w;
  80. px = p.x;
  81. py = p.y;
  82. pz = p.z;
  83. cs = arrow.canUseSideCamera;
  84. ih = arrow.isHit;
  85. ht = arrow.hitType;
  86. if (!inited) {
  87. id = autoID++;
  88. if (autoID > 1000) autoID = 0;
  89. }
  90. inited = true;
  91. }
  92. }
  93. }