using System.Collections; using System.Collections.Generic; using UnityEngine; using Core.Utilities; public class ProjectileMoveScript : MonoBehaviour { public float speed; private float nowSpeed; public float recycleDelay = 0.8f; public float fireRate; public GameObject muzzlePrefab; public GameObject hitPrefab; public AudioClip shotSFX; public AudioClip hitSFX; public List trails; private bool collided; private void Start() { nowSpeed = speed; // if (muzzlePrefab != null) // { // GameObject muzzleObj = Poolable.TryGetPoolable(muzzlePrefab); // ParticleSystem ps = muzzleObj.GetComponent(); // // ps = ps ?? muzzleObj.transform.GetChild(0).GetComponent(); // if (ps == null) // ps = muzzleObj.transform.GetChild(0).GetComponent(); // ps.transform.position = transform.position; // ps.transform.forward = gameObject.transform.forward; // ps.Play(); // StartCoroutine(RecycleParticle(muzzleObj, ps.main.duration)); // } if (muzzlePrefab != null) { var muzzleVFX = Instantiate(muzzlePrefab, transform.position, Quaternion.identity); muzzleVFX.transform.forward = gameObject.transform.forward; var ps = muzzleVFX.GetComponent(); if (ps != null) { ps.Play(); Destroy(muzzleVFX, ps.main.duration); } else { var psChild = muzzleVFX.transform.GetChild(0).GetComponent(); psChild.Play(); Destroy(muzzleVFX, psChild.main.duration); } } if (shotSFX != null && GetComponent()) { GetComponent().PlayOneShot(shotSFX); } // StartCoroutine(RecycleParticle(gameObject, recycleDelay)); } void Update() { if (nowSpeed != 0) transform.position += transform.forward * (nowSpeed * Time.deltaTime); } public IEnumerator RecycleParticle(GameObject particleObj, float waitTime) { yield return new WaitForSeconds(waitTime); // nowSpeed = 0; // ParticleSystem ps = particleObj.GetComponent(); // ps = ps ?? particleObj.transform.GetChild(0).GetComponent(); // ps.Stop(); // Poolable.TryPool(particleObj); nowSpeed = 0; if (trails.Count > 0) { for (int i = 0; i < trails.Count; i++) { trails[i].transform.parent = null; var ps = trails[i].GetComponent(); if (ps != null) { ps.Stop(); Destroy(ps.gameObject, ps.main.duration + ps.main.startLifetime.constantMax); } } } Destroy(gameObject); } }