using System.Collections;
using UnityEngine;
using UnityEngine.AI;

public class MonsterController : MonoBehaviour
{
    public float detectRange = 5.0f;
    public float attackRange = 1.0f;
    public float wanderRadius = 10f;
    private Animator animator;
    private GameObject player;

    private enum MonsterState
    {
        Idle,
        Damaged,
        Attacking
    }
    private MonsterState currentState = MonsterState.Idle;

    private void Start()
    {
        animator = GetComponent<Animator>();
        player = GameObject.FindWithTag("Player");
    }

    private void Update()
    {
        if (currentState == MonsterState.Damaged)
        {
            return; 
        }

        float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);

        if (distanceToPlayer <= attackRange)
        {
            currentState = MonsterState.Attacking;
            LookAtPlayer();
        }
        else if (distanceToPlayer <= detectRange)
        {
            currentState = MonsterState.Idle;
            animator.SetInteger("state", 0);
            LookAtPlayer();
        }
        else
        {
            currentState = MonsterState.Idle;
        }
    }

    private void LookAtPlayer()
    {
        Vector3 direction = (player.transform.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }

    public void OnDamaged()
    {
        currentState = MonsterState.Damaged;
        animator.SetInteger("state", 1);
        //Invoke("EndDamagedState", 1f); 
    }

    public void EndDamagedState()
    {
        currentState = MonsterState.Idle;
        animator.SetInteger("state", 0);
    }
}
using System;
using System.Collections;
using UnityEngine;

namespace test
{
    public class HeroController : MonoBehaviour
    {
        private Vector3 targetPosition;
        private Coroutine moveRoutine;
        private Animator animator;
        private bool isRun;
        public Action onMoveComplete;
        private Transform monsterTarget;
        public float attackRange = 2.0f;

        public void Attack(Transform target)
        {
            monsterTarget = target;
            Move(target.position);
        }

        void Start()
        {
            animator = GetComponent<Animator>();
        }

        public void Move(Vector3 targetPosition)
        {
            this.targetPosition = targetPosition;

            if (this.moveRoutine != null)
            {
                this.StopCoroutine(this.moveRoutine);
            }

            this.moveRoutine = StartCoroutine(this.CoMove());
        }

        private IEnumerator CoMove()
        {
            while (true)
            {
                animator.SetInteger("state", 1);
                this.transform.LookAt(this.targetPosition);
                this.transform.Translate(Vector3.forward * 1f * Time.deltaTime);

                float distance = Vector3.Distance(this.transform.position, this.targetPosition);

                if (monsterTarget != null && Vector3.Distance(this.transform.position, monsterTarget.position) <= attackRange)
                {
                    animator.SetInteger("state", 2);
                    break;
                }
                else if (distance <= 0.1f)
                {
                    animator.SetInteger("state", 0);
                    break;
                }

                yield return null;
            }

            onMoveComplete?.Invoke();
        }

        public void TriggerAttack()
        {
            Debug.Log("TriggerAttack Start!");
            if (monsterTarget != null)
            {
                MonsterController monsterController = monsterTarget.GetComponent<MonsterController>();
                Debug.Log(monsterController);
                Debug.Log("monsterController Attached!");
                if (monsterController != null)
                {
                    Debug.Log("monsterCOntroller != null!");
                    monsterController.OnDamaged();
                }
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using test;

public class Test_PlayerControlSceneMain : MonoBehaviour
{
    [SerializeField]
    private HeroController heroController;

    [SerializeField]
    private Image image;

    void Start()
    {
        this.heroController.onMoveComplete = () =>
        {
            Debug.Log("이동을 완료 했습니다.");
        };
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float maxDistance = 100f;
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, maxDistance))
            {
                if (hit.collider.CompareTag("Monster"))
                {
                    this.heroController.Attack(hit.collider.transform);
                    //this.heroController.TriggerAttack();
                }
                else
                {
                    this.heroController.Move(hit.point);
                }
            }
        }
    }
}

 

+ Recent posts