문제 1

using System.Collections;
using UnityEngine;

namespace Question01
{
    public class Player : MonoBehaviour
    {
        private int speed = 5; // 스피드

        private Animator animator; // 애니메이터 참조
        private BoxCollider2D playerCollider; // 플레이어의 BoxCollider2D 컴포넌트

        private void Start()
        {
            animator = GetComponent<Animator>();
            playerCollider = GetComponent<BoxCollider2D>(); // BoxCollider2D 컴포넌트 참조
            StartCoroutine("CoMove");
        }

        IEnumerator CoMove()
        {
            while (true)
            {
                float h = Input.GetAxisRaw("Horizontal");
                float v = Input.GetAxisRaw("Vertical");

                // 입력 벡터 생성
                Vector2 inputVector = new Vector2(h, v);

                // 입력 벡터 정규화
                if (inputVector.magnitude > 1)
                {
                    inputVector = inputVector.normalized;
                }

                Vector2 currentPosition = transform.position;

                // 카메라 뷰포트를 월드 좌표로 변환
                Vector2 minScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
                Vector2 maxScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

                // BoxCollider2D의 크기를 고려하여 이동 제한 수정
                float playerWidthHalf = playerCollider.size.x * transform.localScale.x / 2;
                float playerHeightHalf = playerCollider.size.y * transform.localScale.y / 2;

                // x좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.x >= maxScreenBounds.x - playerWidthHalf && inputVector.x > 0)
                {
                    inputVector.x = 0; // 오른쪽 이동 제한
                }
                else if (currentPosition.x <= minScreenBounds.x + playerWidthHalf && inputVector.x < 0)
                {
                    inputVector.x = 0; // 왼쪽 이동 제한
                }

                // y좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.y >= maxScreenBounds.y - playerHeightHalf && inputVector.y > 0)
                {
                    inputVector.y = 0; // 위쪽 이동 제한
                }
                else if (currentPosition.y <= minScreenBounds.y + playerHeightHalf && inputVector.y < 0)
                {
                    inputVector.y = 0; // 아래쪽 이동 제한
                }

                // 정규화된 입력 벡터를 사용하여 이동
                Vector2 move = inputVector * speed * Time.deltaTime;
                transform.Translate(move);

                // 애니메이션 상태 제어
                if (inputVector.x > 0)
                {
                    animator.SetInteger("state", 2); // 오른쪽 애니메이션
                }
                else if (inputVector.x < 0)
                {
                    animator.SetInteger("state", 1); // 왼쪽 애니메이션
                }
                else
                {
                    animator.SetInteger("state", 0); // 중앙 애니메이션
                }

                yield return null;
            }
        }
    }

}

 

 

+++

 

문제 2

 

 

using System.Collections;
using UnityEngine;

namespace Question02
{
    public class Player : MonoBehaviour
    {
        private int speed = 5; // 스피드

        private Animator animator; // 애니메이터 참조
        private BoxCollider2D playerCollider; // 플레이어의 BoxCollider2D 컴포넌트

        [SerializeField] GameObject bulletPrefab;
        [SerializeField] Transform firePos;
        [SerializeField] int bulletSpeed;

        private void Start()
        {
            animator = GetComponent<Animator>();
            playerCollider = GetComponent<BoxCollider2D>(); // BoxCollider2D 컴포넌트 참조
            StartCoroutine(CoMove());
            StartCoroutine(CoAttack());
        }

        IEnumerator CoAttack()
        {
            while (true)
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    Instantiate(bulletPrefab, firePos.position, firePos.rotation);
            
                }
                yield return null;
            }
        }

        IEnumerator CoMove()
        {
            while (true)
            {
                float h = Input.GetAxisRaw("Horizontal");
                float v = Input.GetAxisRaw("Vertical");

                // 입력 벡터 생성
                Vector2 inputVector = new Vector2(h, v);

                // 입력 벡터 정규화
                if (inputVector.magnitude > 1)
                {
                    inputVector = inputVector.normalized;
                }

                Vector2 currentPosition = transform.position;

                // 카메라 뷰포트를 월드 좌표로 변환
                Vector2 minScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
                Vector2 maxScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

                // BoxCollider2D의 크기를 고려하여 이동 제한 수정
                float playerWidthHalf = playerCollider.size.x * transform.localScale.x / 2;
                float playerHeightHalf = playerCollider.size.y * transform.localScale.y / 2;

                // x좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.x >= maxScreenBounds.x - playerWidthHalf && inputVector.x > 0)
                {
                    inputVector.x = 0; // 오른쪽 이동 제한
                }
                else if (currentPosition.x <= minScreenBounds.x + playerWidthHalf && inputVector.x < 0)
                {
                    inputVector.x = 0; // 왼쪽 이동 제한
                }

                // y좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.y >= maxScreenBounds.y - playerHeightHalf && inputVector.y > 0)
                {
                    inputVector.y = 0; // 위쪽 이동 제한
                }
                else if (currentPosition.y <= minScreenBounds.y + playerHeightHalf && inputVector.y < 0)
                {
                    inputVector.y = 0; // 아래쪽 이동 제한
                }

                // 정규화된 입력 벡터를 사용하여 이동
                Vector2 move = inputVector * speed * Time.deltaTime;
                transform.Translate(move);

                // 애니메이션 상태 제어
                if (inputVector.x > 0)
                {
                    animator.SetInteger("state", 2); // 오른쪽 애니메이션
                }
                else if (inputVector.x < 0)
                {
                    animator.SetInteger("state", 1); // 왼쪽 애니메이션
                }
                else
                {
                    animator.SetInteger("state", 0); // 중앙 애니메이션
                }

                yield return null;
            }
        }
    }

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

namespace Question02
{
    public class Bullet : MonoBehaviour
    {
        [SerializeField] private int speed;
        void Update()
        {
            transform.Translate(Vector2.up * speed * Time.deltaTime);
        }
    }
}

 

 

+++

 

문제 3

 

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


namespace Question03
{
    public class EnemyGenerator : MonoBehaviour
    {

        [SerializeField] GameObject[] Enemy;

        [SerializeField] Transform[] spawnPoints;


        void Start()
        {
            StartCoroutine(EnemyCreate());
        }


        IEnumerator EnemyCreate()
        {
            while (true)
            {
                Vector2 spawnPosition = new Vector2(Random.Range(spawnPoints[0].position.x, spawnPoints[1].position.x), spawnPoints[0].position.y);
                int randN = Random.Range(0, 2);

                Instantiate(Enemy[randN], spawnPosition, Quaternion.identity);

                yield return new WaitForSeconds(1);
                yield return null;
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Question03
{
    public class Enemy : MonoBehaviour
    {
        [SerializeField] private int speed;
        void Update()
        {
            this.transform.Translate(Vector2.down * speed * Time.deltaTime);
        }
    }
}

 

+++

 

문제 4

 

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace Question04
{
    public class Enemy : MonoBehaviour
    {
        [SerializeField] private int speed;
        [SerializeField] int enemyHp = 1;


        private Animator anim;
        enemyState state;


        public enum enemyState
        {
            Idle, GetHit, Die
        }


        
        private void Start()
        {
            anim = GetComponent<Animator>();
            StartCoroutine(CoAnim());
        }

        void Update()
        {
            if(enemyHp <= 0)
            {
                speed = 0;
                Destroy(this.GetComponent<Collider2D>());
                state = enemyState.Die;

            }

            this.transform.Translate(Vector2.down * speed * Time.deltaTime);
        }

        void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Bullet"))
            {
                Destroy(collision.gameObject);
                if (this.enemyHp > 0)
                {
                    this.enemyHp--;

                    // HP가 남아있다면 GetHit 애니메이션 실행 후 Idle로 복귀
                    state = enemyState.GetHit; // 상태를 GetHit로 변경
                    anim.Play("GetHit", -1, 0); // 이미 Hit 중이라면 애니메이션 재시작
                }
            }

        }

        IEnumerator CoAnim()
        {
            while (true)
            {
                switch (state)
                {
                    case enemyState.Idle:
                        anim.SetInteger("state", 0); // Idle
                        break;
                    case enemyState.GetHit:
                        anim.SetInteger("state", 1);
                        yield return new WaitUntil(() => anim.GetCurrentAnimatorStateInfo(0).IsName("GetHit") && //GetHit 애니메이션이 끝날때 까지 기다림
                                            anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f);
                        state = enemyState.Idle;
                        break;
                    case enemyState.Die:
                        anim.SetInteger("state", 2); // Die
                        yield return new WaitUntil(() => anim.GetCurrentAnimatorStateInfo(0).IsName("Die") &&
                                               anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f);
                        Destroy(this.gameObject);
                        break;
                }
                if (state == enemyState.Die) break;
                yield return null;
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace Question04
{
    public class EnemyGenerator : MonoBehaviour
    {

        [SerializeField] GameObject[] Enemy;

        [SerializeField] Transform[] spawnPoints;


        void Start()
        {
            StartCoroutine(EnemyCreate());
        }


        IEnumerator EnemyCreate()
        {
            while (true)
            {
                Vector2 spawnPosition = new Vector2(Random.Range(spawnPoints[0].position.x, spawnPoints[1].position.x), spawnPoints[0].position.y);
                int randN = Random.Range(0, 2);

                Instantiate(Enemy[randN], spawnPosition, Quaternion.identity);

                yield return new WaitForSeconds(1);
                yield return null;
            }
        }
    }
}
using System.Collections;
using UnityEngine;

namespace Question04
{
    public class Player : MonoBehaviour
    {
        private int speed = 5; // 스피드

        private Animator animator; // 애니메이터 참조
        private BoxCollider2D playerCollider; // 플레이어의 BoxCollider2D 컴포넌트

        [SerializeField] GameObject bulletPrefab;
        [SerializeField] Transform firePos;

        private void Start()
        {
            animator = GetComponent<Animator>();
            playerCollider = GetComponent<BoxCollider2D>(); // BoxCollider2D 컴포넌트 참조
            StartCoroutine(CoMove());
            StartCoroutine(CoAttack());
        }

        IEnumerator CoAttack()
        {
            while (true)
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    Instantiate(bulletPrefab, firePos.position, firePos.rotation);
            
                }
                yield return null;
            }
        }

        IEnumerator CoMove()
        {
            while (true)
            {
                float h = Input.GetAxisRaw("Horizontal");
                float v = Input.GetAxisRaw("Vertical");

                // 입력 벡터 생성
                Vector2 inputVector = new Vector2(h, v);

                // 입력 벡터 정규화
                if (inputVector.magnitude > 1)
                {
                    inputVector = inputVector.normalized;
                }

                Vector2 currentPosition = transform.position;

                // 카메라 뷰포트를 월드 좌표로 변환
                Vector2 minScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
                Vector2 maxScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

                // BoxCollider2D의 크기를 고려하여 이동 제한 수정
                float playerWidthHalf = playerCollider.size.x * transform.localScale.x / 2;
                float playerHeightHalf = playerCollider.size.y * transform.localScale.y / 2;

                // x좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.x >= maxScreenBounds.x - playerWidthHalf && inputVector.x > 0)
                {
                    inputVector.x = 0; // 오른쪽 이동 제한
                }
                else if (currentPosition.x <= minScreenBounds.x + playerWidthHalf && inputVector.x < 0)
                {
                    inputVector.x = 0; // 왼쪽 이동 제한
                }

                // y좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.y >= maxScreenBounds.y - playerHeightHalf && inputVector.y > 0)
                {
                    inputVector.y = 0; // 위쪽 이동 제한
                }
                else if (currentPosition.y <= minScreenBounds.y + playerHeightHalf && inputVector.y < 0)
                {
                    inputVector.y = 0; // 아래쪽 이동 제한
                }

                // 정규화된 입력 벡터를 사용하여 이동
                Vector2 move = inputVector * speed * Time.deltaTime;
                transform.Translate(move);

                // 애니메이션 상태 제어
                if (inputVector.x > 0)
                {
                    animator.SetInteger("state", 2); // 오른쪽 애니메이션
                }
                else if (inputVector.x < 0)
                {
                    animator.SetInteger("state", 1); // 왼쪽 애니메이션
                }
                else
                {
                    animator.SetInteger("state", 0); // 중앙 애니메이션
                }

                yield return null;
            }
        }
    }

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

namespace Question04
{
    public class Bullet : MonoBehaviour
    {
        [SerializeField] private int speed;
        void Update()
        {
            transform.Translate(Vector2.up * speed * Time.deltaTime);
        }
    }
}

 

 

+++

 

문제5

 

 

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

namespace Question05
{
    public class Boom : MonoBehaviour
    {
       
        void Update()
        {
            Destroy(gameObject,2);
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Enemy"))
            {
                collision.GetComponent<Enemy>().enemyHp = 0; 
            }
        }
    }
}
using System.Collections;
using UnityEngine;

namespace Question05
{
    public class Player : MonoBehaviour
    {
        private int speed = 5; // 스피드

        private Animator animator; // 애니메이터 참조
        private BoxCollider2D playerCollider; // 플레이어의 BoxCollider2D 컴포넌트

        [SerializeField] GameObject boomPrefab;
        [SerializeField] Transform firePos;

        private void Start()
        {
            animator = GetComponent<Animator>();
            playerCollider = GetComponent<BoxCollider2D>(); // BoxCollider2D 컴포넌트 참조
            StartCoroutine(CoMove());
            StartCoroutine(CoAttack());
        }

        IEnumerator CoAttack()
        {
            while (true)
            {
                if (Input.GetKeyDown(KeyCode.B))
                {
                    Instantiate(boomPrefab, firePos.position, firePos.rotation);
            
                }
                yield return null;
            }
        }

        IEnumerator CoMove()
        {
            while (true)
            {
                float h = Input.GetAxisRaw("Horizontal");
                float v = Input.GetAxisRaw("Vertical");

                // 입력 벡터 생성
                Vector2 inputVector = new Vector2(h, v);

                // 입력 벡터 정규화
                if (inputVector.magnitude > 1)
                {
                    inputVector = inputVector.normalized;
                }

                Vector2 currentPosition = transform.position;

                // 카메라 뷰포트를 월드 좌표로 변환
                Vector2 minScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
                Vector2 maxScreenBounds = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

                // BoxCollider2D의 크기를 고려하여 이동 제한 수정
                float playerWidthHalf = playerCollider.size.x * transform.localScale.x / 2;
                float playerHeightHalf = playerCollider.size.y * transform.localScale.y / 2;

                // x좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.x >= maxScreenBounds.x - playerWidthHalf && inputVector.x > 0)
                {
                    inputVector.x = 0; // 오른쪽 이동 제한
                }
                else if (currentPosition.x <= minScreenBounds.x + playerWidthHalf && inputVector.x < 0)
                {
                    inputVector.x = 0; // 왼쪽 이동 제한
                }

                // y좌표 제한 검사, 플레이어의 크기를 고려
                if (currentPosition.y >= maxScreenBounds.y - playerHeightHalf && inputVector.y > 0)
                {
                    inputVector.y = 0; // 위쪽 이동 제한
                }
                else if (currentPosition.y <= minScreenBounds.y + playerHeightHalf && inputVector.y < 0)
                {
                    inputVector.y = 0; // 아래쪽 이동 제한
                }

                // 정규화된 입력 벡터를 사용하여 이동
                Vector2 move = inputVector * speed * Time.deltaTime;
                transform.Translate(move);

                // 애니메이션 상태 제어
                if (inputVector.x > 0)
                {
                    animator.SetInteger("state", 2); // 오른쪽 애니메이션
                }
                else if (inputVector.x < 0)
                {
                    animator.SetInteger("state", 1); // 왼쪽 애니메이션
                }
                else
                {
                    animator.SetInteger("state", 0); // 중앙 애니메이션
                }

                yield return null;
            }
        }
    }

}

+ Recent posts