유니티 심화

HeroShooter - 튜토리얼 스테이지

다모아 2023. 8. 22. 18:15

 

FadeOutMain

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

namespace TutorialScene
{
    public class FadeOutMain : MonoBehaviour
    {
        [SerializeField]
        private Image dim;

        public void FadeOutAndSceneManager()
        {
            this.dim.gameObject.SetActive(true);
            this.StartCoroutine(this.CoFadeOutAndSceneManager());
        }

        private IEnumerator CoFadeOutAndSceneManager()
        {
            var color = this.dim.color;
            while (true)
            {
                color.a += 0.01f;
                this.dim.color = color;
                if(color.a >= 1)
                {
                    break;
                }

                yield return null;
            }
            SceneManager.LoadScene("Stage1Scene");
        }
    }
}

 

PlayerController

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

namespace TutorialScene
{
    public class PlayerController : MonoBehaviour
    {
        private enum eAnimState
        {
            Idle, RunF, RunB, RunR, RunL
        }
        [SerializeField]
        private VariableJoystick joystick; //조이스틱 가져오기
        [SerializeField]
        private GameObject portalGo;
        [SerializeField]
        private DoorController leftDoorController;
        [SerializeField]
        private DoorController RightDoorController;
        [SerializeField]
        private FadeOutMain fadeOutMain;
        private Animator anim;
        // Start is called before the first frame update
        void Start()
        {
            this.anim = this.GetComponent<Animator>();
        }

        // Update is called once per frame
        void Update()
        {
            //조이스틱을 마우스로 누르면 플레이어가 그 방향으로 움직인다.
            Vector3 moveDir = (Vector3.forward * this.joystick.Direction.y) + (Vector3.right * this.joystick.Direction.x);
            this.transform.Translate(moveDir.normalized * 3f * Time.deltaTime);

            //좌,우,아래,위,정지 애니메이션 구현
            if(moveDir.x > 0)
            {
                //오른쪽
                this.PlayAnimation(eAnimState.RunR);
            }
            else if(moveDir.x < 0)
            {
                //왼쪽
                this.PlayAnimation(eAnimState.RunL);
            }
            else if(moveDir.z > 0)
            {
                //위
                this.PlayAnimation(eAnimState.RunF);
            }
            else if(moveDir.z < 0)
            {
                //아래
                this.PlayAnimation(eAnimState.RunB);
            }
            else
            {
                //정지
                this.PlayAnimation(eAnimState.Idle);
            }
        }

        private void PlayAnimation(eAnimState state)
        {
            this.anim.SetInteger("State", (int)state);
        }
        private void OnCollisionEnter(Collision collision)
        {
            if (collision.collider.tag == "Door")
            {
                fadeOutMain.FadeOutAndSceneManager();
            }
            else if (collision.collider.tag == "Portal")
            {
                Destroy(this.portalGo);
                leftDoorController.OpenDoor();
                RightDoorController.OpenDoor();
            }
        }
    }
}

 

DoorController

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

namespace TutorialScene
{
    public class DoorController : MonoBehaviour
    {
        private Animator anim;
        // Start is called before the first frame update
        void Start()
        {
            this.anim = this.GetComponent<Animator>();
        }

        public void OpenDoor()
        {
            this.anim.SetInteger("State", 1);
        }
    }
}

 

CameraController

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

namespace TutorialScene
{
    public class CameraController : MonoBehaviour
    {
        [SerializeField]
        private Transform playerTr;
        [SerializeField]
        private float distance = 10.0f;
        [SerializeField]
        private float height = 15.0f;
        [SerializeField]
        private float damping = 3.0f;

        private Vector3 velocity = Vector3.zero;
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            Vector3 tpos = new Vector3(this.transform.position.x, this.transform.position.y, this.playerTr.transform.position.z - this.distance);

            if (this.transform.position.z <= -6.8f)
            {
                //위 아래로 가는 방향만 따라다니기
                this.transform.position = Vector3.SmoothDamp(this.transform.position, tpos, ref velocity, this.damping);
            }
        }
    }
}

1. 조이스틱으로 캐릭터 움직이기
2. 지정된 위치까지 이동하면 포탈열림
3. 다음 스테이지로 이동
4. 조이스틱에 손을 떼면 적을향해 사격을 시작
벽이 있을경우 공격 못함
5. 시야에 들어오면 적을 선택
6. 타겟을 잡았다면 자동 공격 , 적이 죽을 때까지
7. 총알을 다 쓰면 리로드 , 리로드할 때는 공격할 수 없음
8. 리로드할 때는 공격할 수는 없지만 움직일 수는 있음
9. 모든 몬스터를 다 죽이면 다음스테이지가 열림
10. 3스테이지까지는 움직이지 않는 몬스터 [설치된 몬스터만 있음]
11. 몬스터 종류에 총알을 발사하는 몬스터도 있음
12. 일정시간마다 플레이어에게 총알을 발사

튜토리얼 - 스테이지1 - 스테이지2 - 스테이지3 - 보스

튜토리얼 : 지정된 위치까지 이동해서 스테이지 전환
스테이지1 : 움직이지 않고 공격도 안하는 몬스터 배치
스테이지2 : 움직이지 않고 원거리 공격을 하는 몬스터 배치
스테이지3 : 움직이고 근거리 공격하는 몬스터 배치
스테이지4 : 움직이고 원거리 공격을 하는 몬스터 배치
보스 : 이동하면서 공격하는 몬스터 배치

4시간 동안 -> 조이스틱 구현, 포탈열림


조이스틱으로 캐릭터 움직이기 [완]
카메라가 플레이어 따라다니기 [카메라가 플레이어 위 아래로 가는 방향만 따라다님] [완]

포탈구현 [완]