| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PortalsController : MonoBehaviour
- {
- public PortalsController targetPortal; //传送目标
- public List<Rigidbody> rbs = new List<Rigidbody>(); //通过当前传送门的单位
- public float TransmitCD; //每个单位传送的CD
- public bool isReverse; //传送门朝向是否相反
- public bool onlyEnemy; //是否只能传送敌人
- private void OnTriggerEnter(Collider other)
- {
- if (targetPortal == null)
- {
- return;
- }
- //传送主角、使魔、敌人
- BeSearchTrigger beSearchTrigger = other.GetComponent<BeSearchTrigger>();
- if (beSearchTrigger != null)
- {
- Rigidbody rb = beSearchTrigger.GetComponentInParent<Rigidbody>();
- if(rb.tag == "Demonic")
- {
- return;
- }
- if(rb.tag == "Player" && onlyEnemy)
- {
- return;
- }
- if (Transmit(rb))
- {
- MoveCharacter moveCharacter = rb.GetComponent<MoveCharacter>();
- if (moveCharacter != null)
- {
- moveCharacter.portalsController = this;
- moveCharacter.transmitTime = TransmitCD;
- moveCharacter.haveTransmit = true;
- }
- }
- return;
- }
- //传送子弹
- Bullet bullet = other.GetComponent<Bullet>();
- if(bullet != null && bullet.canTransmit)
- {
- Rigidbody rb = bullet.GetComponentInParent<Rigidbody>();
- if (Transmit(rb))
- {
- bullet.portalsController = this;
- bullet.transmitTime = TransmitCD;
- bullet.haveTransmit = true;
- }
- return;
- }
- //传送魂
- Soul soul = other.GetComponent<Soul>();
- if(soul != null)
- {
- Rigidbody rb = soul.GetComponent<Rigidbody>();
- if (Transmit(rb))
- {
- soul.portalsController = this;
- soul.transmitTime = TransmitCD;
- soul.haveTransmit = true;
- }
- return;
- }
- }
- //传送
- public bool Transmit(Rigidbody rb)
- {
-
- if (!targetPortal.rbs.Exists(t => t == rb))
- {
- if (!rbs.Exists(t => t == rb))
- {
- if ((transform.parent.localScale.x > 0 && rb.linearVelocity.x > 0) ||
- (transform.parent.localScale.x < 0 && rb.linearVelocity.x < 0))
- {
- rbs.Add(rb);
- Vector3 targetPos = targetPortal.transform.position;
- if (targetPortal.transform.parent.localScale.x > 0)
- {
- targetPos.x -= 1;
- }
- else
- {
- targetPos.x += 1;
- }
-
- targetPos.y += rb.transform.position.y - transform.position.y;
- rb.transform.position = targetPos;
- if (!isReverse)
- {
- Vector3 velocity = rb.linearVelocity;
- velocity.x = -velocity.x;
- rb.linearVelocity = velocity;
- }
- return true;
- }
- }
- }
- return false;
- }
- }
|