유니티 심화

HeroShooter - FadeIn, FloatingJoystick, 닭이미지, 텍스트, Ray

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

PlayerController

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

public class PlayerController : MonoBehaviour
{
    private enum eAnimState
    {
        Idle, RunF
    }

    [SerializeField]
    private CJoystick joystick; //조이스틱 가져오기
    [SerializeField]
    private GameObject portalGo;
    [SerializeField]
    private DoorController leftDoorController;
    [SerializeField]
    private DoorController RightDoorController;
    [SerializeField]
    private FadeOutMain fadeOutMain;
    [SerializeField]
    private Text text;
    [SerializeField]
    private float radius = 10f;

    private Animator anim;

    void Start()
    {
        this.anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //Ray로 한번 발견이나 해보자
        Ray ray = new Ray(this.transform.position, this.transform.forward);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, 1000f))
        {
            DrawArrow.ForDebug(this.transform.position, hit.collider.transform.position, 2f, Color.red, ArrowType.Solid);
            if (hit.collider.tag == "Monster")
            {
                Debug.Log("몬스터");
            }
        }
        //조이스틱을 마우스로 누르면 플레이어가 그 방향으로 움직인다.
        float h = this.joystick.Horizontal;
        float v = this.joystick.Vertical;
        Vector3 moveDir = new Vector3(h, 0, v);

        //애니메이션 구현
        if (moveDir == Vector3.zero)
        {
            this.PlayAnimation(eAnimState.Idle);
        }
        else
        {
            float angle = Mathf.Atan2(moveDir.x, moveDir.z) * Mathf.Rad2Deg;
            Quaternion angleRotation = Quaternion.AngleAxis(angle, Vector3.up);

            this.transform.rotation = angleRotation;

            this.PlayAnimation(eAnimState.RunF);

            this.transform.Translate(Vector3.forward * 5f * Time.deltaTime);
        }
        
    }

    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();
            this.text.text = "좋습니다. 문을 통해 \n계속 이동하세요.";
        }
    }
}

 

Stage1SceneMain

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

public class Stage1SceneMain : MonoBehaviour
{
    [SerializeField]
    private Image dim;

    private void Start()
    {
        this.StartCoroutine(this.CoFadeInAndSceneManager());
    }

    private IEnumerator CoFadeInAndSceneManager()
    {
        Color color = this.dim.color;
        while (true)
        {
            color.a -= 0.01f;
            this.dim.color = color;

            if(color.a <= 0)
            {
                break;
            }
            yield return null;
        }
    }
}