| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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>();
- }
- return;
- }
- //传送子弹
- Bullet bullet = other.GetComponent<Bullet>();
- if(bullet != null && bullet.canTransmit)
- {
- Rigidbody rb = bullet.GetComponentInParent<Rigidbody>();
- if (Transmit(rb))
- {
- bullet.transmitTime = TransmitCD;
- bullet.haveTransmit = true;
- }
- return;
- }
- //传送魂
- Soul soul = other.GetComponent<Soul>();
- if(soul != null)
- {
- Rigidbody rb = soul.GetComponent<Rigidbody>();
- 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.velocity.x > 0) ||
- (transform.parent.localScale.x < 0 && rb.velocity.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.velocity;
- velocity.x = -velocity.x;
- rb.velocity = velocity;
- }
- return true;
- }
- }
- }
- return false;
- }
- }
|