EasyCastleUNITY

2D 탄막 게임 1 본문

유니티 심화

2D 탄막 게임 1

EasyCastleT 2023. 8. 16. 18:21

1. 플레이어 이동 및 공격(총알 만들기)

2. 적 피격 애니메이션, 사망 애니메이션 

 

전체적인 화면

BulletGenerator

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

public class BulletGenerator : MonoBehaviour
{
    [SerializeField]
    private Transform BulletInitPos; //총알 생성 위치
    [SerializeField]
    private GameObject bulletPrefab; //총알 프리팹

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject go = Instantiate(bulletPrefab);
            go.transform.position = this.BulletInitPos.position;
        }
    }
}

BulletControl

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

public class BulletControl : MonoBehaviour
{
    private Transform transform;
    private float moveSpeed = 3.0f;
    // Start is called before the first frame update
    void Start()
    {
        this.transform = this.GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(Vector2.up * this.moveSpeed * Time.deltaTime);
        this.SelfDestory();
    }

    private void SelfDestory()
    {
        if(this.transform.position.y >4.8f)
        Destroy(this.gameObject);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("<color=yellow>총알 삭제</color>");
        Destroy(this.gameObject);
    }
}

PlayerControl

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

public class PlayerControl : MonoBehaviour
{
    enum eState
    {
        Idle, Left , Right
    }
    private Transform trans; //트랜스폼
    private Animator anim; //애니메이터 
    [SerializeField]
    private float moveSpeed = 2.0f; //이동속도
    
    // Start is called before the first frame update
    void Start()
    {
        this.trans =GetComponent<Transform>();
        this.anim = GetComponent<Animator>(); 
        // 0: Idle, 1:Left 2:Right
    }

    // Update is called once per frame
    void Update()
    {
        this.trans.position = this.ClampPosition(this.trans.position);
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector2 moveDir = (Vector2.up * v) + (Vector2.right * h);
        this.trans.Translate(moveDir.normalized*this.moveSpeed*Time.deltaTime);

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.PlayAnimation(eState.Left);
        } 
        else if(Input.GetKey(KeyCode.RightArrow)) {
            this.PlayAnimation(eState.Right);
        }
        else
        {
            this.PlayAnimation(eState.Idle);
        }

    }

    private Vector2 ClampPosition (Vector2 position)
    {
        return new Vector2(Mathf.Clamp(position.x, -2.3f, 2.3f),
            Mathf.Clamp(position.y, -4.5f, 4.5f));
    }

    private void PlayAnimation(eState state)
    {
        int istate = (int)state;
        this.anim.SetInteger("State", istate);
    }
}

EnemyControl

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

public class EnemyControl : MonoBehaviour
{
    private Animator anim;
    private Coroutine routine;
    public System.Action onDie;
    [SerializeField]
    private int hp = 5;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (this.hp <= 0)
        {
            this.Die();
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("<color=red>적 피격!</color>");
        hp--;
        this.PlayAnimation();
    }

    private void SelfDestroy()
    {
        Destroy(this.gameObject);
    }

    private void Die()
    {
        this.onDie();
        StartCoroutine(CoDie());
    }
    private IEnumerator CoDie()
    {
        yield return new WaitForSeconds(0.167f);
        this.SelfDestroy();
        yield return null;
    }
    private void PlayAnimation()
    {
        if(this.routine != null)
        {
            this.StopCoroutine(this.routine);
        }
        this.routine = StartCoroutine(CoPlayAnimation());
    }

    private IEnumerator CoPlayAnimation()
    {
        this.anim.SetBool("State", true);
        yield return new WaitForSeconds(0.167f);
        this.anim.SetBool("State", false);
        yield return null;

    }
}

GameMain

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

public class GameMain : MonoBehaviour
{
    [SerializeField]
    private GameObject explosionPrefab;
    [SerializeField]
    private EnemyControl enemy;

    // Start is called before the first frame update
    void Start()
    {
        this.enemy.onDie = () =>{
            GameObject go = Instantiate(explosionPrefab);
            go.transform.position = enemy.transform.position;
            this.RemoveExplosion(go);
        };
    }

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

    private void RemoveExplosion(GameObject go)
    {
        StartCoroutine(this.CoRemoveExplosion(go));
    }
    private IEnumerator CoRemoveExplosion(GameObject go)
    {
        yield return new WaitForSeconds(0.5f);
        Destroy(go);
        yield return null;
    }
}

전체적인 모습