PortalsController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PortalsController : MonoBehaviour
  5. {
  6. public PortalsController targetPortal; //传送目标
  7. public List<Rigidbody> rbs = new List<Rigidbody>(); //通过当前传送门的单位
  8. public float TransmitCD; //每个单位传送的CD
  9. public bool isReverse; //传送门朝向是否相反
  10. public bool onlyEnemy; //是否只能传送敌人
  11. private void OnTriggerEnter(Collider other)
  12. {
  13. if (targetPortal == null)
  14. {
  15. return;
  16. }
  17. //传送主角、使魔、敌人
  18. BeSearchTrigger beSearchTrigger = other.GetComponent<BeSearchTrigger>();
  19. if (beSearchTrigger != null)
  20. {
  21. Rigidbody rb = beSearchTrigger.GetComponentInParent<Rigidbody>();
  22. if(rb.tag == "Demonic")
  23. {
  24. return;
  25. }
  26. if(rb.tag == "Player" && onlyEnemy)
  27. {
  28. return;
  29. }
  30. if (Transmit(rb))
  31. {
  32. MoveCharacter moveCharacter = rb.GetComponent<MoveCharacter>();
  33. if (moveCharacter != null)
  34. {
  35. moveCharacter.portalsController = this;
  36. moveCharacter.transmitTime = TransmitCD;
  37. moveCharacter.haveTransmit = true;
  38. }
  39. }
  40. return;
  41. }
  42. //传送子弹
  43. Bullet bullet = other.GetComponent<Bullet>();
  44. if(bullet != null && bullet.canTransmit)
  45. {
  46. Rigidbody rb = bullet.GetComponentInParent<Rigidbody>();
  47. if (Transmit(rb))
  48. {
  49. bullet.portalsController = this;
  50. bullet.transmitTime = TransmitCD;
  51. bullet.haveTransmit = true;
  52. }
  53. return;
  54. }
  55. //传送魂
  56. Soul soul = other.GetComponent<Soul>();
  57. if(soul != null)
  58. {
  59. Rigidbody rb = soul.GetComponent<Rigidbody>();
  60. if (Transmit(rb))
  61. {
  62. soul.portalsController = this;
  63. soul.transmitTime = TransmitCD;
  64. soul.haveTransmit = true;
  65. }
  66. return;
  67. }
  68. }
  69. //传送
  70. public bool Transmit(Rigidbody rb)
  71. {
  72. if (!targetPortal.rbs.Exists(t => t == rb))
  73. {
  74. if (!rbs.Exists(t => t == rb))
  75. {
  76. if ((transform.parent.localScale.x > 0 && rb.linearVelocity.x > 0) ||
  77. (transform.parent.localScale.x < 0 && rb.linearVelocity.x < 0))
  78. {
  79. rbs.Add(rb);
  80. Vector3 targetPos = targetPortal.transform.position;
  81. if (targetPortal.transform.parent.localScale.x > 0)
  82. {
  83. targetPos.x -= 1;
  84. }
  85. else
  86. {
  87. targetPos.x += 1;
  88. }
  89. targetPos.y += rb.transform.position.y - transform.position.y;
  90. rb.transform.position = targetPos;
  91. if (!isReverse)
  92. {
  93. Vector3 velocity = rb.linearVelocity;
  94. velocity.x = -velocity.x;
  95. rb.linearVelocity = velocity;
  96. }
  97. return true;
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. }