본문 바로가기
KDT/유니티 심화

23/08/20 [주말과제] 궁수의 전설 공격까지 만들기

by 잰쟁 2023. 8. 20.
728x90

** 최대한 비슷한 에셋을 찾으려고 했으나 발견하지 못하여 적당한 에셋으로 선택!
** 기존 게임과는 다르게 캐릭터가 바라보는 방향이 오른쪽이여서 오른쪽으로 발사하게 만들었다.

 

(+ 아쉬운점)

- 방향전환을 넣으면 자꾸 멈춰있을때 캐릭터가 사라져서..ㅜㅜ 빼버렸다

- 총알이 너무 무더기로 발사되는데 어떻게 해야 한 발씩 나가는걸까..

(기존에는 스페이스바로 조절할 수 있었는데 이건 조이스틱이라 잘 모르겠다..)

 

 

GameMain

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

public class GameMain : MonoBehaviour
{
    [SerializeField]
    private GameObject enemyPrefab1;
    [SerializeField]
    private GameObject enemyPrefab2;
    [SerializeField]
    private GameObject enemyPrefab3;

    // Start is called before the first frame update
    void Start()
    {
        this.Create();
    }

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

    //몬스터 오브젝트 생성
    private void Create()
    {
        GameObject enemyGo1 = Instantiate(this.enemyPrefab1);
        GameObject enemyGo2 = Instantiate(this.enemyPrefab2);
        GameObject enemyGo3 = Instantiate(this.enemyPrefab3);
    }
}

PlayerController

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

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private Joystick joystick;
    [SerializeField]
    private GameObject bulletPrefab;

    private Animator anim;
    public float moveSpeed = 5.0f;


    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
    }

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

        //조이스틱의 방향을 받아오기
        float h = this.joystick.Direction.x;
        float v = this.joystick.Direction.y;

        //이동시키기
        Vector3 dir = new Vector3(h,v,0).normalized;
        Debug.Log(dir);
        this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
        
        //이동상태에 따라 동작 통제
        if (dir == Vector3.zero)
        {
            //총알 생성
            GameObject bulletGo = Instantiate(this.bulletPrefab);
            //총알 위치 조정하기
            Vector3 bulletOffset = new Vector3(0.75f, -0.1f, 0);
            bulletGo.transform.position = this.transform.position + bulletOffset;

            //애니메이션 적용
            this.anim.SetInteger("State", 0);
        }
        else
        {       
            //화면 밖으로 나가지 않으면서 이동
            var clampX = Mathf.Clamp(this.transform.position.x, -2.1f, 2.1f);
            var clampY = Mathf.Clamp(this.transform.position.y, -4.2f, 4.2f);
            this.transform.position = new Vector3(clampX, clampY, 0);

            //애니메이션 적용
            this.anim.SetInteger("State", 1);
        }      
    } 
}

BulletController

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

public class BulletController : MonoBehaviour
{
    public float shootSpeed = 3.0f;

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

    // Update is called once per frame
    void Update()
    {
        this.Move();

        //화면밖으로 나가면 제거하기
        if(this.transform.position.x > 2.4f)
        {
            Destroy(this.gameObject);
        }
    }

    //총알 이동(슈팅) 메서드
    private void Move()
    {
        this.transform.Translate(Vector2.right * this.shootSpeed * Time.deltaTime);
    }
    
    //충돌시 제거 메서드
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Hit!");
        Destroy(this.gameObject);
    }
}

EnemyController

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

public class EnemyController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    //총알과 충돌시 제거
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("사망!");
        Destroy(this.gameObject);
    }
}

 

 

+) 구름에 닿으면 Scene 전환

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

namespace GameScene1 
{
    public class PlayerController : MonoBehaviour
    {
        [SerializeField]
        private Joystick joystick;
        [SerializeField]
        private GameObject bulletPrefab;

        private Animator anim;
        public float moveSpeed = 5.0f;


        // Start is called before the first frame update
        void Start()
        {
            this.anim = this.GetComponent<Animator>();
        }

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

            //조이스틱의 방향을 받아오기
            float h = this.joystick.Direction.x;
            float v = this.joystick.Direction.y;

            //이동시키기
            Vector3 dir = new Vector3(h, v, 0).normalized;
            Debug.Log(dir);
            this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);

            //이동상태에 따라 동작 통제
            if (dir == Vector3.zero)
            {
                //총알 생성
                GameObject bulletGo = Instantiate(this.bulletPrefab);
                //총알 위치 조정하기
                Vector3 bulletOffset = new Vector3(0.75f, -0.1f, 0);
                bulletGo.transform.position = this.transform.position + bulletOffset;

                //애니메이션 적용
                this.anim.SetInteger("State", 0);
            }
            else
            {
                //화면 밖으로 나가지 않으면서 이동
                var clampX = Mathf.Clamp(this.transform.position.x, -2.1f, 2.1f);
                var clampY = Mathf.Clamp(this.transform.position.y, -4.2f, 4.2f);
                this.transform.position = new Vector3(clampX, clampY, 0);

                //애니메이션 적용
                this.anim.SetInteger("State", 1);
            }
        }

        //씬 전환 메서드
        private void OnTriggerEnter2D(Collider2D collision)
        {
            Debug.Log("다음 Scene으로 전환!");
            SceneManager.LoadScene("GameScene2");
        }
    }
}

 

+) 아쉬운 점s

- 씬 전환을 한 이후에 player가 조이스틱으로 움직이지 않는 에러

(기존 PlayerController에 namespace까지 걸었는데도 안 움직인다.. 속상.....)