PortalsController.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  34. return;
  35. }
  36. //传送子弹
  37. Bullet bullet = other.GetComponent<Bullet>();
  38. if(bullet != null && bullet.canTransmit)
  39. {
  40. Rigidbody rb = bullet.GetComponentInParent<Rigidbody>();
  41. if (Transmit(rb))
  42. {
  43. bullet.transmitTime = TransmitCD;
  44. bullet.haveTransmit = true;
  45. }
  46. return;
  47. }
  48. //传送魂
  49. Soul soul = other.GetComponent<Soul>();
  50. if(soul != null)
  51. {
  52. Rigidbody rb = soul.GetComponent<Rigidbody>();
  53. return;
  54. }
  55. }
  56. //传送
  57. public bool Transmit(Rigidbody rb)
  58. {
  59. if (!targetPortal.rbs.Exists(t => t == rb))
  60. {
  61. if (!rbs.Exists(t => t == rb))
  62. {
  63. if ((transform.parent.localScale.x > 0 && rb.velocity.x > 0) ||
  64. (transform.parent.localScale.x < 0 && rb.velocity.x < 0))
  65. {
  66. rbs.Add(rb);
  67. Vector3 targetPos = targetPortal.transform.position;
  68. if (targetPortal.transform.parent.localScale.x > 0)
  69. {
  70. targetPos.x -= 1;
  71. }
  72. else
  73. {
  74. targetPos.x += 1;
  75. }
  76. targetPos.y += rb.transform.position.y - transform.position.y;
  77. rb.transform.position = targetPos;
  78. if (!isReverse)
  79. {
  80. Vector3 velocity = rb.velocity;
  81. velocity.x = -velocity.x;
  82. rb.velocity = velocity;
  83. }
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. }