드디어 플레이어의 투척을 완성했다. 그동안 정확하게 알고 쓰지 못했던 부분에 대해서 조금씩 더 알아가는 느낌이었다.
원래는 Player 스크립트에서 오브젝트를 생성하고 던지는 작업까지 했다면, 스크립트를 분할하여 던지는 작업을 하는 로직을 따로 구현하고 Player 스크립트에는 오브젝트를 생성하는 작업만 추가하였다.
Player 수정
public void CreateGrenade()
{
if (hasGrenades == 0)
return;
// 투척물 오브젝트를 생성하고 Grenade를 가져옴
GameObject instantGrenade = Instantiate(grenadeObj, throwPoint.position, transform.rotation);
Grenade grenade = instantGrenade.GetComponent<Grenade>();
if (grenade != null)
{
grenade.Init();
}
}
<Grenade> : 수정중
public class Grenade : MonoBehaviour
{
public GameObject[] grenades;
public int hasGrenades;
public GameObject grenadeObj;
public Rigidbody rigid;
public GameObject meshObj;
public GameObject effectObj;
public void Init()
{
Debug.Log("Init 들어왔다");
rigid = GetComponent<Rigidbody>();
if (rigid != null)
{
// Vector3.up * (높이값) + transform.forward * 전진 힘 => 포물선
float throwForce = 5f; // 던지는 힘 설정
float throwHeight = 10f;
rigid.AddForce(Vector3.up * throwHeight + transform.forward * throwForce, ForceMode.Impulse);
}
StartCoroutine(Explosion());
}
IEnumerator Explosion()
{
yield return new WaitForSeconds(3f);
rigid.velocity = Vector3.zero;
rigid.angularVelocity = Vector3.zero;
meshObj.SetActive(false);
effectObj.SetActive(true);
RaycastHit[] rayHits = Physics.SphereCastAll(transform.position, 15, Vector3.up, 0f, LayerMask.GetMask("Enemy"));
foreach(RaycastHit hitObj in rayHits)
{
//hitObj.transform.GetComponent<Enemy>().HitByGrenade(transform.position);
}
// statemachine 이 idamageable 인터페이스 => takeeffect, takedamage
// attacker는 플레이어로, attackeffecttype은 넉백으로, value로 값 지정가능
Destroy(gameObject, 3);
}
}
원래는 생성된 오브젝트에서 Ray를 쏴서 충돌하는 충돌체가 Enemy라는 Layer를 가지면 넉백을 시키도록 구현하려 했으나, 그렇게 되면 Enemy 스크립트를 건드려야 하기도 하고 다른 분이 작업 중이시면 컨플릭트가 날 확률이 있기 때문에 팀원분과 상의를 한 결과, 이전에 만들어둔 인터페이스를 활용해서 넉백을 구현할 수 있기 때문에 EnemyState에 접근하고 인터페이스 값에서 채워야 하는 타입과 변수들을 지정하면 사용할 수 있다고 하셨다. 아직 다른 분이 짜신 로직을 이해하지 못했기 때문에 좀 더 분석해 보고 구현해 보아야겠다.
'TIL (since 2023.08.07 ~ )' 카테고리의 다른 글
2023-11-21 TIL(Unity 최종 프로젝트 22일차) (0) | 2023.11.21 |
---|---|
2023-11-20 TIL(Unity 최종 프로젝트 21일차) (0) | 2023.11.20 |
2023-11-15 TIL(Unity 최종 프로젝트 18일차) (0) | 2023.11.15 |
2023-11-14 TIL(Unity 최종 프로젝트 17일차) (0) | 2023.11.14 |
2023-11-13 TIL(Unity 최종 프로젝트 16일차) (0) | 2023.11.13 |