C#/Unity

[Unity2D] 몬스터만들기, 애니메이션, 애니메이터(이동, 대기, 공격, 죽음)

반나무 2020. 7. 8. 17:26

캐릭터와 몬스터를 움직이는데는 Animation, Animator가 필요하다

 

Animation을 하나씩 만들어 Animator에 붙여 사용한다.

 

Animator : 상태 관리자

Animation : 각각의 상태

 

GreenMonster.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 이동간 절대기준 사용위한 Tweening 라이브러리 사용
using DG.Tweening;
using System;

public class GreenMonster : MonoBehaviour
{

    bool _IsAction = false;
    bool _IsDeath = false;
    bool _IsAttack = false;

    bool _DontMoveDown = false;
    bool _DontMoveUp = false;
    bool _DontMoveLeft = false;
    bool _DontMoveRight = false;

    int _HitCount = 0;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (!_IsAction && !_IsDeath && !_IsAttack) {
            
            int R = UnityEngine.Random.Range(0, 4);
            switch (R)
            {
                case 0:
                    LeftMove();
                    break;
                case 1:
                    RightMove();
                    break;
                case 2:
                    UpMove();
                    break;
                case 3:
                    DownMove();
                    break;
            }
        }
    }

    // 위로
    void UpMove()
    {
        if (_DontMoveUp == false)
        {
            float EndPositionY = transform.position.y;
            _IsAction = true; // 토큰 값 변경

            GetComponent<Animator>().SetInteger("State", 0);

            EndPositionY += 1.5f;
            transform.DOMoveY(EndPositionY, 1.5f).OnComplete(ActionComplete).SetEase(Ease.Flash);

        }
    }

    void DownMove()
    {
        if (_DontMoveDown == false)
        {
            float EndPositionY = transform.position.y;
            _IsAction = true; // 토큰 값 변경

            GetComponent<Animator>().SetInteger("State", 1);

            EndPositionY -= 1.5f;
            transform.DOMoveY(EndPositionY, 1.5f).OnComplete(ActionComplete).SetEase(Ease.Flash);
        }
    }
    void LeftMove()
    {
        if (_DontMoveLeft == false)
        {
            _IsAction = true; // 토큰 값 변경
            float EndPositionX = transform.position.x;
            // 내부 컴포넌트는 GetComponent로 불러올 수 있다.
            GetComponent<Animator>().SetInteger("State", 2);  // Animator에서 State값 가져와 변경하는 코드

            EndPositionX -= 1.5f;

            transform.DOMoveX(EndPositionX, 1.5f).OnComplete(ActionComplete).SetEase(Ease.Flash);
        }

           
    }
    // 오른쪽이동
    void RightMove()
    {
        if (_DontMoveRight == false)
        {
            float EndPositionX = transform.position.x;
            _IsAction = true; // 토큰 값 변경

            GetComponent<Animator>().SetInteger("State", 3);

            EndPositionX += 1.5f;
            transform.DOMoveX(EndPositionX, 1.5f).OnComplete(ActionComplete).SetEase(Ease.Flash);
        }
        
    }
    /// 이동 마무리
    /// /////////////////////////////////////////////////////////////////////////////
    /// 이동 끝났을때 실행되는 메소드
    void ActionComplete()
    {
        // 토큰값 false
        _IsAction = false;

    }  

    void AttackComplete()
    {
        _IsAttack = false;
    }
    
    // 트리거로 함
    private void OnTriggerEnter2D(Collider2D collision)
    {
        String name = collision.gameObject.name;

        // collosion은 상대방의 정보 가져옴
        if (name == "bot_wall")
        {
            _DontMoveDown = true;
        }
        else if (name == "top_wall")
        {
            _DontMoveUp = true;
        }
        else if (name == "left_wall")
        {
            _DontMoveLeft = true;
        }
        else if (name == "right_wall")
        {
            _DontMoveRight = true;
        } 
        else if(name == "Player_Bullet(Clone)")
        {
            // Hit
            GetComponent<Animator>().SetInteger("State", GetComponent<Animator>().GetInteger("State")%4 + (4*2));
            
            _HitCount++;
            
            // Death
            if (_HitCount >= 3)
            {
                _IsDeath = true;
                
                // 곱해서 더한값을 나머지 연산으로 다시 처음으로 돌아감(방향 파악하기위해)
                GetComponent<Animator>().SetInteger("State", GetComponent<Animator>().GetInteger("State")%4 + (4 * 3));

            }
        } 
        else if(name == "hero")
        {
            _IsAttack = true;
            GetComponent<Animator>().SetInteger("State", GetComponent<Animator>().GetInteger("State") % 4 + (4 * 4));

        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        String name = collision.gameObject.name;
        // collosion은 상대방의 정보 가져옴
        if (name == "bot_wall")
        {
            _DontMoveDown = false;
        }
        else if (name == "top_wall")
        {
            _DontMoveUp = false;
        }
        else if (name == "left_wall")
        {
            _DontMoveLeft = false;
        }
        else if (name == "right_wall")
        {
            _DontMoveRight = false;
        }
        else if(name == "hero")
        {
            AttackComplete();
        }
    }

    private void MonsterDestroy()
    {
        Destroy(gameObject);
    }
}
반응형