YLBeamAdon.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Adon.Game.BO;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace AdonGameKit
  6. {
  7. /// <summary>
  8. /// 言灵射线
  9. /// </summary>
  10. public class YLBeamAdon : MonoBehaviour
  11. {
  12. RaycastHit hitPoint;
  13. /// <summary>
  14. /// 最大射线长度
  15. /// </summary>
  16. public float MaxBeamLength = 30;
  17. public LayerMask layerMask;
  18. /// <summary>
  19. /// 射线对象
  20. /// </summary>
  21. public Transform rayBeam;
  22. /// <summary>
  23. /// 冲击效果对象
  24. /// </summary>
  25. public Transform rayImpact;
  26. /// <summary>
  27. /// 枪口效果对象
  28. /// </summary>
  29. public Transform rayMuzzle;
  30. public Transform hitEff;
  31. public BaseYLPartAdon m_YL;//释放者挂件(言灵)
  32. public BaseHero m_Hero;//释放者(英雄)
  33. public float m_FireTimeMax = 0.5f;
  34. public string m_CurUseSkill;
  35. float beamLength;
  36. bool isFXSpawned = false; // 碰撞光效只生产一次
  37. float fireTime;
  38. //Transform newRayBeam;
  39. void Awake()
  40. {
  41. fireTime = m_FireTimeMax;
  42. //hitEff = transform.Find("Monster_Trajectend");
  43. rayBeam = transform.Find("Monster_Traject");
  44. layerMask = 1 << LayerMask.NameToLayer("Enemy");
  45. }
  46. // Start is called before the first frame update
  47. void Start()
  48. {
  49. //newRayBeam = Instantiate(rayBeam);
  50. //newRayBeam.gameObject.SetActive(false);
  51. fireTime = m_FireTimeMax;
  52. hitEff.gameObject.SetActive(false);
  53. }
  54. void FixedUpdate()
  55. {
  56. Raycast();
  57. }
  58. void Raycast()//射线
  59. {
  60. hitPoint = new RaycastHit();
  61. Ray ray = new Ray(transform.position, transform.forward);//射线
  62. if (Physics.Raycast(ray, out hitPoint, MaxBeamLength, layerMask))//如果射线有碰撞
  63. {
  64. beamLength = Vector3.Distance(transform.position, hitPoint.point);//根据距离算得射线长度
  65. rayBeam.transform.localScale = new Vector3(1,1, beamLength);
  66. if (rayImpact)//冲击效果位置
  67. {
  68. rayImpact.position = hitPoint.point + transform.forward * 0.1f;
  69. rayImpact.gameObject.SetActive(true);
  70. }
  71. DefendMonster defend = hitPoint.transform.GetComponent<DefendMonster>();
  72. if (defend != null)
  73. {
  74. Debug.Log("打中了防御罩");
  75. //计时,周期发送伤害
  76. if (fireTime >= m_FireTimeMax)
  77. {
  78. fireTime = 0;
  79. //实例化克隆防御光效
  80. Transform effobj = Instantiate(defend.m_DefendHitObj);
  81. effobj.gameObject.AddComponent<DestroyObjectDelayed>();
  82. effobj.position = hitPoint.point;//设置爆炸位置
  83. effobj.gameObject.SetActive(true);
  84. Transform effobj2 = Instantiate(hitEff);
  85. effobj2.gameObject.AddComponent<DestroyObjectDelayed>();
  86. effobj2.position = hitPoint.point;//设置爆炸位置
  87. effobj2.gameObject.SetActive(true);
  88. }
  89. }
  90. else
  91. {
  92. //newRayBeam.gameObject.SetActive(true);
  93. //计时,周期发送伤害
  94. if (fireTime >= m_FireTimeMax)
  95. {
  96. fireTime = 0;
  97. //if (!isFXSpawned)//执行一次
  98. //{
  99. //isFXSpawned = true;
  100. BaseMonsterAdon component = hitPoint.transform.GetComponent<BaseMonsterAdon>();
  101. if (component != null)
  102. {
  103. Transform effobj2 = Instantiate(hitEff);
  104. effobj2.gameObject.AddComponent<DestroyObjectDelayed>();
  105. effobj2.position = hitPoint.point;//设置爆炸位置
  106. effobj2.gameObject.SetActive(true);
  107. //发送伤害消息
  108. //返回false能量不足,打断技能
  109. bool bo = X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(
  110. m_Hero.mData.UID, m_CurUseSkill,
  111. true, m_YL.M_YLType, component.mData.UID, false);
  112. if (bo == false)
  113. {
  114. m_YL.m_CurUseSkill = "";
  115. m_YL.OffFire();
  116. }
  117. }
  118. }
  119. }
  120. fireTime += Time.deltaTime;
  121. }
  122. else
  123. {
  124. hitEff.gameObject.SetActive(false);
  125. //newRayBeam.gameObject.SetActive(false);
  126. // 设置射线为最大长度
  127. beamLength = MaxBeamLength;
  128. rayBeam.transform.localScale = new Vector3(1, 1, beamLength);
  129. //lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength));
  130. //设置爆炸位置
  131. if (rayImpact)
  132. rayImpact.position = transform.position + transform.forward * beamLength;
  133. }
  134. //设置枪口光效位置
  135. if (rayMuzzle)
  136. rayMuzzle.position = transform.position + transform.forward * 0.1f;
  137. }
  138. }
  139. }