您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
102 行
2.6 KiB
102 行
2.6 KiB
using UnityEngine;
|
|
|
|
|
|
//This script handles the logic to determine whether an active projectile
|
|
//...should be remain active after it has been shot by an enemy
|
|
public class Projectile : MonoBehaviour
|
|
{
|
|
public float aliveTime;
|
|
[HideInInspector] public Rigidbody rb;
|
|
// private ObstacleTowerAgent agent;
|
|
public bool selfDestructNow;
|
|
public float maxTimeToLive = 3;
|
|
public float pauseCollisionDetectionWaitTime = .5f;
|
|
// [HideInInspector] public ShootProjectiles projectileController;
|
|
public PoolGameObjects impactParticlePool;
|
|
public TrailRenderer trailRend;
|
|
void Awake()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
// agent = FindObjectOfType<ObstacleTowerAgent>();
|
|
impactParticlePool = FindObjectOfType<PoolGameObjects>();
|
|
}
|
|
|
|
|
|
void OnEnable()
|
|
{
|
|
if (!rb)
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
aliveTime = 0;
|
|
selfDestructNow = false;
|
|
if (trailRend)
|
|
{
|
|
trailRend.Clear(); ;
|
|
}
|
|
// if (agent)
|
|
// {
|
|
// agent.CompletedFloorAction += SelfDestruct;
|
|
// }
|
|
}
|
|
|
|
|
|
void OnDisable()
|
|
{
|
|
aliveTime = 0;
|
|
// if (agent)
|
|
// {
|
|
// agent.CompletedFloorAction -= SelfDestruct;
|
|
// }
|
|
}
|
|
|
|
//Turn the projectile off
|
|
void SelfDestruct()
|
|
{
|
|
gameObject.SetActive(false);
|
|
// rb.velocity = Vector3.zero;
|
|
// rb.angularVelocity = Vector3.zero;
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
// if (
|
|
// (agent && agent.IsDone()) //if the agent is done projectiles can die
|
|
// || aliveTime > maxTimeToLive //we lived too long. time to die
|
|
// )
|
|
if (aliveTime > maxTimeToLive) //we lived too long. time to die
|
|
{
|
|
selfDestructNow = true;
|
|
}
|
|
|
|
if (selfDestructNow)
|
|
{
|
|
SelfDestruct();
|
|
}
|
|
|
|
aliveTime += Time.fixedDeltaTime;
|
|
}
|
|
|
|
|
|
void OnCollisionEnter(Collision col)
|
|
{
|
|
if (aliveTime > pauseCollisionDetectionWaitTime)
|
|
{
|
|
selfDestructNow = true;
|
|
SelfDestruct();
|
|
|
|
}
|
|
|
|
foreach (var item in impactParticlePool.poolList)
|
|
{
|
|
if (!item.gameObject.activeInHierarchy)
|
|
{
|
|
item.transform.position = rb.transform.position;
|
|
item.gameObject.SetActive(true);
|
|
return;
|
|
}
|
|
}
|
|
// print(col.gameObject.name);
|
|
}
|
|
}
|