0408 타겟 포인트로 달려가서 적 때리기

2021. 4. 8. 16:45unity

using UnityEngine;
using UnityEngine.UI;

public class UIGame : MonoBehaviour
{
    public Button btnAttack;
    public Button btnRun;
    public Text Hp;
    private void Start()
    {
        
    }
    private void Update()
    {
        this.Hp.GetComponent<Text>().text = this.Hp.ToString();
    }
}

using UnityEngine;

public class InGame : MonoBehaviour
{
    public Transform TargetPoint;
    public UIGame uiGame;
    public CharacterController player;
    public CharacterController enemy;
    // Start is called before the first frame update
    void Start()
    {
        this.uiGame.btnAttack.onClick.AddListener(() =>
        {
            this.player.Attack(this.enemy);
        });

        this.uiGame.btnRun.onClick.AddListener(() =>
        {
            this.player.Move(this.TargetPoint);
        });
        this.uiGame.btnRun.onClick.AddListener(() =>
        {
            this.enemy.Move(this.TargetPoint);
        });
        this.uiGame.btnAttack.onClick.AddListener(() =>
        {
            this.enemy.PlayAnimation("damage");
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public System.Action OnMoveComplete;
    public System.Action OnAttackComplete;
    public CharacterController player;
    public CharacterController enemy;
    private Animation animation;
    private Vector3 targetPosition;
    private bool isRun = false;
    private bool isAttack = false;
    private int enemyhp=100;
    private int enemymaxHp;
    private int damage = 10;
    // Start is called before the first frame update
    void Start()
    {
        this.animation = GetComponent<Animation>();
        this.enemyhp = this.enemymaxHp;
    }
    
    public void PlayAnimation(string animName)
    {
        this.animation.Play(animName);
    }
    public void Move(Transform targetPoint)
    {
        this.targetPosition = targetPoint.position;
        this.isRun = true;
        this.PlayAnimation("run@loop");
    }
    public void Attack(CharacterController enemy) 
    {
        this.enemy = enemy;
        this.animation.Play("attack_sword_01");
        this.isAttack = true;
    }
    public void GetDamaged() 
    {
        if (this.isAttack == true) 
        {
            this.enemyhp -= 10;
            if (this.enemyhp <= 0)
            {
                this.enemy.PlayAnimation("die");
            }
            else 
            {
                this.enemy.PlayAnimation("damage");
            }
        }
        
    }
    public void OnTriggerEnter(Collider other)
    {
        GetComponent<Rigidbody>().isKinematic = true;
        
    }

    // Update is called once per frame
    void Update()
    {
        if (this.isRun==true)
        {
            //매프레임마다 이동 
            //방향 * 속도 
            //1 * 0.01
            
            this.transform.Translate(0, 0, 0.008f);

            //방향 벡터 구하기 
            var dir = this.targetPosition - this.transform.position;
            //벡터의 길이구하기 
            var distance = dir.magnitude;
            Debug.Log(distance);
            if (distance <= 0.15)
            {
                //멈추고 
                this.isRun = false;
               
                //애니메이션 실행 
                this.PlayAnimation("idle@loop");
            }
        }
    }
}