금일은 오전을 Asset을 탐색 및 구매하는데 일정을 보냈고 , 오후에는 해당 Asset을 코드리뷰하는 시간을 가졌다.
투사체 이펙트 관련 Asset에서 레퍼런스로 준 코드를 활용하여 PlayerProjectile에 적용하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerProjectile : MonoBehaviour
{
[SerializeField] protected float speed = 15f;
[SerializeField] protected float hitOffset = 0f;
[SerializeField] protected bool UseFirePointRotation;
[SerializeField] protected Vector3 rotationOffset = new Vector3(0, 0, 0);
[SerializeField] protected GameObject hit;
[SerializeField] protected ParticleSystem hitPS;
[SerializeField] protected GameObject flash;
[SerializeField] protected Rigidbody rb;
[SerializeField] protected Collider col;
[SerializeField] protected Light lightSourse;
[SerializeField] protected GameObject[] Detached;
[SerializeField] protected ParticleSystem projectilePS;
private bool startChecker = false;
[SerializeField] protected bool notDestroy = false;
public Vector3 dir;
public LayerMask TargetLayer;
protected virtual void Start()
{
if (!startChecker)
{
/*lightSourse = GetComponent<Light>();
rb = GetComponent<Rigidbody>();
col = GetComponent<Collider>();
if (hit != null)
hitPS = hit.GetComponent<ParticleSystem>();*/
if (flash != null)
{
flash.transform.parent = null;
}
}
if (notDestroy)
StartCoroutine(DisableTimer(5));
else
Destroy(gameObject, 5);
startChecker = true;
}
protected virtual IEnumerator DisableTimer(float time)
{
yield return new WaitForSeconds(time);
if (gameObject.activeSelf)
gameObject.SetActive(false);
yield break;
}
protected virtual void OnEnable()
{
if (startChecker)
{
if (flash != null)
{
flash.transform.parent = null;
}
if (lightSourse != null)
lightSourse.enabled = true;
col.enabled = true;
rb.constraints = RigidbodyConstraints.None;
}
}
protected virtual void FixedUpdate()
{
if (speed != 0)
{
rb.velocity = dir * speed;
}
}
private void OnCollisionEnter(Collision collision)
{
if (TargetLayer == ((1 << collision.gameObject.layer) | TargetLayer))
{
Debug.Log($"플레이어의 공격이 {collision.gameObject.name}에 충돌");
//Lock all axes movement and rotation
rb.constraints = RigidbodyConstraints.FreezeAll;
//speed = 0;
if (lightSourse != null)
lightSourse.enabled = false;
col.enabled = false;
projectilePS.Stop();
projectilePS.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
ContactPoint contact = collision.contacts[0];
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point + contact.normal * hitOffset;
//Spawn hit effect on collision
if (hit != null)
{
hit.transform.rotation = rot;
hit.transform.position = pos;
if (UseFirePointRotation) { hit.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 180f, 0); }
else if (rotationOffset != Vector3.zero) { hit.transform.rotation = Quaternion.Euler(rotationOffset); }
else { hit.transform.LookAt(contact.point + contact.normal); }
hitPS.Play();
}
//Removing trail from the projectile on cillision enter or smooth removing. Detached elements must have "AutoDestroying script"
foreach (var detachedPrefab in Detached)
{
if (detachedPrefab != null)
{
ParticleSystem detachedPS = detachedPrefab.GetComponent<ParticleSystem>();
detachedPS.Stop();
}
}
if (notDestroy)
StartCoroutine(DisableTimer(hitPS.main.duration));
else
{
gameObject.SetActive(false);
}
ObjectPoolManager.Instance.GetPool("playerProjectile", Utils.POOL_KEY_PLAYERPROJECTILE).GetObject();
}
}