유니티

04/08 유니티 연습

박준희 2021. 4. 8. 18:15

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    private Animation anim;
    private Vector3 targetPosition;
    public EnemyController enemy;
    private bool isAttack;
    private bool isRun;
    private float delta;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animation>();
    }

    public void PlayAnimation(string animName)
    {
        this.anim.Play(animName);        
    }

    public void Move(Transform targetPoint)
    {
        this.targetPosition = targetPoint.position;
        this.isRun = true;
    }
 
    private void Update()
    {
        if (this.isRun)
        {
            //매프레임마다 이동 
            //방향 * 속도 
            //1 * 0.01
            this.transform.Translate(0, 0, 0.01f);

            //방향 벡터 구하기 
            var dir = this.targetPosition - this.transform.position;
            //벡터의 길이구하기 
            var distance = dir.magnitude;
            
            if (distance <= 0.4f)
            {
                //멈추고 
                this.isRun = false;
                this.isAttack = true;
            }
        }
        if(!this.anim.isPlaying && !enemy.animation.isPlaying)
        {
            this.isAttack = true;
        }
        if(this.isAttack)
        {
            this.delta += Time.deltaTime;
            if(enemy.hp > 0)
            {
                this.PlayAnimation("attack_sword_01");
                
                if (this.delta >= 0.38f)
                {
                    enemy.Hit();
                    this.isAttack = false;
                    this.delta = 0;
                }                
                
            }
            else
            {
                this.PlayAnimation("idle@loop");
            }
        }
    }

}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    public int hp = 100;
    public Animation animation;
    // Start is called before the first frame update
    void Start()
    {
        this.animation = this.GetComponent<Animation>();
    }

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

    public int Hit()
    {
        animation.Play("damage");
        hp = hp - 10;
        if(hp == 0)
        {
            animation.Play("die");
        }
        Debug.Log(hp);
        return hp;
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InGame : MonoBehaviour
{
    public UIGame uiGame;
    public CharacterController player;    
    public Transform targetPoint;
    // Start is called before the first frame update
    void Start()
    {
        this.uiGame.btnAttack.onClick.AddListener(() => {
            this.player.PlayAnimation("attack_sword_01");            
        });
        this.uiGame.btnRun.onClick.AddListener(() => {
            this.player.PlayAnimation("run@loop");
            this.player.Move(targetPoint);
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIGame : MonoBehaviour
{
    public Button btnAttack;
    public Button btnRun;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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