3D 콘텐츠 제작
zombero- boss 2일
노재두내
2023. 10. 8. 18:07
적 캐릭터(boss) 제작하기!!
내비게이션
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class BossController : MonoBehaviour
{
private Transform playerTrans;
private Transform bossTrans;
private NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
this.agent = this.GetComponent<NavMeshAgent>();
this.bossTrans = this.GetComponent<Transform>();
this.playerTrans=GameObject.FindWithTag("Player").GetComponent<Transform>();
agent.destination = this.playerTrans.position;
}
}
--> player가 위치를 바꾸면 따라오지는 않음 (start할때 한번만 이동 -> coroutine으로 계속 이동하도록함)
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.AI;
public class BossController : MonoBehaviour
{
public enum eState
{
IDLE,TRACE,ATTACK,DIE
}
public eState state;
[SerializeField]
private float attackRange = 2.0f;
[SerializeField]
private float traceRange = 10.0f;
public bool isDie;
private Transform playerTrans;
private Transform bossTrans;
private NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
this.agent = this.GetComponent<NavMeshAgent>();
this.bossTrans = this.GetComponent<Transform>();
this.playerTrans=GameObject.FindWithTag("Player").GetComponent<Transform>();
//agent.destination = this.playerTrans.position;
this.StartCoroutine(CheckMonsterState());
this.StartCoroutine(MonsterAction());
}
private IEnumerator CheckMonsterState()
{
while (!isDie)
{
yield return new WaitForSeconds(0.3f);
float distance = Vector3.Distance(this.playerTrans.position, this.bossTrans.position);
if (distance <= attackRange)
{
state = eState.ATTACK;
}
else if (distance <= traceRange)
{
state = eState.TRACE;
}
else
{
state = eState.IDLE;
}
}
}
private IEnumerator MonsterAction()
{
while (this.isDie == false)
{
switch (this.state)
{
case eState.IDLE:
this.agent.isStopped = true;
break;
case eState.ATTACK:
break;
case eState.TRACE:
this.agent.SetDestination(this.playerTrans.position);
this.agent.isStopped = false;
break;
case eState.DIE:
break;
}
yield return new WaitForSeconds(0.3f);
}
}
private void OnDrawGizmos()
{
if (state == eState.TRACE)
{
Gizmos.color = Color.blue;
GizmosExtensions.DrawWireArc(this.transform.position, this.transform.forward, 360, this.traceRange);
}
else if (state == eState.ATTACK)
{
Gizmos.color = Color.red;
GizmosExtensions.DrawWireArc(this.transform.position, this.transform.forward, 360, this.attackRange);
}
}
}
--> 플레이어가 traceRange내에 있으면 계속 플레이어를 따라감
애니메이션