YLDistanceBulletAdon.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Adon.Game.BO;
  2. using Adon.Game.Helper;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace AdonGameKit
  7. {
  8. /// <summary>
  9. /// 言灵距离形子弹
  10. /// </summary>
  11. public class YLDistanceBulletAdon : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// 子弹步进速度
  15. /// </summary>
  16. public float m_Velocity = 10;
  17. /// <summary>
  18. /// 子弹最大距离
  19. /// </summary>
  20. public float m_DistanceMax = 10;
  21. /// <summary>
  22. /// 攻击距离
  23. /// </summary>
  24. public float m_AttackRange = 5;
  25. /// <summary>
  26. /// 生命周期
  27. /// </summary>
  28. public float m_LifeTime = 2f;
  29. public BaseYLPartAdon m_YL;//释放者挂件(言灵)
  30. public BaseHero m_Hero;//释放者(英雄)
  31. public string m_CurUseSkill;
  32. public Transform m_HitEff;//受击光效
  33. /// <summary>
  34. /// 子弹当前距离
  35. /// </summary>
  36. private float curDistance = 0;
  37. bool isFXSpawned = false; // 碰撞光效只生产一次
  38. float timer = 0f; // 子弹生命周期计时
  39. void Awake()
  40. {
  41. }
  42. // Start is called before the first frame update
  43. void Start()
  44. {
  45. }
  46. void FixedUpdate()
  47. {
  48. //计算距离,达到距离则悬停,添加开启碰撞检测
  49. if (!isFXSpawned)//执行一次
  50. {
  51. if (curDistance < m_DistanceMax)//距离计算
  52. {
  53. Vector3 step = transform.forward * Time.deltaTime * m_Velocity;//子弹步进
  54. curDistance += Vector3.Distance(transform.position, (transform.position + step));//距离增加
  55. transform.position += step;//向前步进
  56. }
  57. else
  58. {
  59. //达到距离则执行攻击
  60. isFXSpawned = true;
  61. //查找周围敌人
  62. List<GameObject> monsters = PlayerHelperAdon.GetNearEnemysDistance(this.gameObject, m_AttackRange);
  63. for (int i = 0; i < monsters.Count; i++)
  64. {
  65. BaseMonsterAdon component = monsters[i].gameObject.GetComponent<BaseMonsterAdon>();
  66. //发送伤害消息
  67. X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(m_Hero.mData.UID, m_YL.m_CurUseSkill, true, m_YL.M_YLType, component.mData.UID, false);
  68. //释放受击效果
  69. Transform hitEffObj = Instantiate(m_HitEff);
  70. hitEffObj.gameObject.SetActive(true);
  71. hitEffObj.position = component.m_HitObj.position;
  72. hitEffObj.LookAt(transform.position);
  73. hitEffObj.eulerAngles = new Vector3(0,hitEffObj.eulerAngles.y-180,0);
  74. //hitEffObj.rotation = Quaternion.Euler(component.transform.position - transform.position);
  75. //hitEffObj.LookAt(transform);
  76. if (!hitEffObj.GetComponent<DestroyObjectDelayed>()) hitEffObj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  77. }
  78. }
  79. }
  80. // 生命周期到了则销毁
  81. if (timer >= m_LifeTime)
  82. OnProjectileDestroy();
  83. timer += Time.deltaTime;
  84. }
  85. /// <summary>
  86. /// 子弹销毁
  87. /// </summary>
  88. void OnProjectileDestroy()
  89. {
  90. //销毁,后期改为对象池
  91. Destroy(this.gameObject);
  92. }
  93. }
  94. }