HeroShoot-튜토리얼 씬 수정(조이스틱, fadeOut,Gate에 Slerp)
2023. 8. 23.

joystick 변경

-TutorialMain.cs를 수정해 player의 이동을 수정하였다.

조이스틱은 TutorialMain.cs에서 관리하되,

moveDir을 매개변수에 저장해 PlayerController에서 이동하는 함수를 호출하도록하였다.

 

+애니메이션 추가 후 결과(카메라 코드 수정을 위해 이동 코드 비활성화 상태)

문 오픈에 Slerp를 추기위해 코드 수정

-처음에 Slerp에 this.timeCount에 저장하는게 아니라 Time.deltaTime을 바로 집어넣었더니 문이 제대로 열리지 않고 rotation이 1내외에서 움직이기만 했다.

-timeCount에 while문이 실행되는 동안 Time.deltaTime을 더해주고, 그 값을 Slerp에 넣어주어 해결했다.

 

 

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

public class MyJoyStick : FloatingJoystick
{
    public System.Action onDown;
    public System.Action onUp;

    protected override void Start()
    {
        base.Start();
        this.background.gameObject.SetActive(true);
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        this.onDown();
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        this.background.gameObject.SetActive(true);
        this.background.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -722f);
        this.onUp();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerController : MonoBehaviour
{
    //[SerializeField] private VariableJoystick joystick;
  
    [SerializeField] private float moveSpeed = 3f;
    private TutorialMain tutorialMain;
  //  private Vector3 downPosition;
    //private bool isDown = false;
    public Animator anim;
    
    // Start is called before the first frame update
    void Start()
    {
      this.tutorialMain = FindObjectOfType<TutorialMain>();

    }

    // Update is called once per frame
    void Update()
    {
       
    }
    public void movePlayer(Vector3 dir)
    {
        float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;

        this.transform.Translate(dir.normalized * this.moveSpeed * Time.deltaTime, Space.World);
        this.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.up);//축으로 회전
        this.anim.SetInteger("State", 1);//run forward animation
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.CompareTag("Portal"))
        {
            Debug.Log("Portal");
            Destroy(collision.gameObject);
            this.tutorialMain.OpenGate();
        }
        else if (collision.collider.CompareTag("Stage1"))
        {
            this.tutorialMain.imgDim.gameObject.SetActive(true);
            StartCoroutine(this.tutorialMain.CoFadeOut());//fade out
          
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TutorialMain : MonoBehaviour
{
    public Image imgDim;

    [SerializeField] private GameObject gateLeftGo;
    [SerializeField] private GameObject gateRightGo;
    [SerializeField] private GameObject npcGo;
    [SerializeField] private GameObject speechGo;
    [SerializeField] private MyJoyStick joystick;
    private Text text;
    private bool isDown;
    private float openDamping =1f;
    private float timeCount;
    private PlayerController playerController;
    // Start is called before the first frame update
    void Start()
    {
        this.playerController = FindAnyObjectByType<PlayerController>();

        this.joystick.onDown = () => {
            this.isDown = true;
        };
        this.joystick.onUp = () => {
            this.isDown = false;
            this.playerController.anim.SetInteger("State", 0);
            //  this.player.Idle();
        };

        //시작시 안내 UI Active.
        this.npcGo.SetActive(true);
        this.speechGo.SetActive(true);
        this.text = this.speechGo.GetComponentInChildren<Text>();
        this.text.text = "목표 지점까지 이동하세요!";
    }
    // Update is called once per frame
    void Update()
    {
        if (this.isDown)
        {
            float h = this.joystick.Direction.x;
            float v = this.joystick.Direction.y;

            Vector3 moveDir = (Vector3.forward * v) + Vector3.right * h;

            //조이스틱으로 입력받아 방향 바라보게함
            if (moveDir != Vector3.zero)
            {//이동
                this.playerController.movePlayer(moveDir);
            }
            else
            {
                this.playerController.anim.SetInteger("State", 0);
                //idle
            }
        }
    }

    public void OpenGate()
    {
        Debug.Log("OpenGate");
        StartCoroutine(this.CoOpenGate());
  
    }
    private IEnumerator CoOpenGate()
    {
        //this.gateLeftGo.transform.Rotate(0,90,0);
        var left = Quaternion.Euler(new Vector3(0, 90, 0));
        var right = Quaternion.Euler(new Vector3(0, -90, 0));
        var leftRotation = this.gateLeftGo.transform.rotation;
        var rightRotation = this.gateRightGo.transform.rotation;
        while (true)
        {
            this.gateLeftGo.transform.rotation = Quaternion.Slerp(leftRotation, left,this.timeCount*this.openDamping);
            this.gateRightGo.transform.rotation = Quaternion.Slerp(rightRotation, right, this.timeCount * this.openDamping);
            this.timeCount += Time.deltaTime;
            // this.gateRightGo.transform.Rotate(0,-90, 0);
            
            if(this.gateLeftGo.transform.localRotation == left)
            {
                break;
            }
            yield return null;
        }
        this.text.text = "잘했어요! 문이 열렸으니 이동해볼까요?";
    }
    public IEnumerator CoFadeOut()
    {
        var color = this.imgDim.color;

        while (true)
        {
            color.a += 0.01f;
            this.imgDim.color = color;
            if (color.a >= 1)
            {
                break;
            }
            yield return null;
        }
        Debug.Log("<color=yellow>다음씬으로 이동</color>");
        Debug.Log("Stage1");
        SceneManager.LoadScene("Stage1");
    }
}

 

 

 

 

myoskin