HeroShoot-1. 튜토리얼 씬(캐릭터 이동/회전, 특정 위치 도달시 포탈 오픈)
2023. 8. 22.

 

 

조이스틱으로 캐릭터 움직이기

=> transform.Translate 사용.

PlayerController-조이스틱으로 캐릭터 움직이기

지정된 위치까지 이동하면 포탈열림/ 다음스테이지로 이동

=>OnCollisonEnter를 사용했다. 포탈과 문에 각각 collider를 달아주고, CompareTag를 통해 태그를 구별한다.

 

https://docs.unity3d.com/ScriptReference/Component.CompareTag.html

 

Unity - Scripting API: Component.CompareTag

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

캔버스에 이미지 넣어서 안내하는 캐릭터 만들기

setNativeSize를 누르면 원사이즈로 들어간다.

TutorialMain.cs

 

-Play하자마자 비활성화되어 있던 두 이미지 객체를 활성화한다.

 -speech_bubble 오브젝트의 자식인 Text의 컴포넌트를 불러오기 위해 GetComponentInChildren을 사용.

 

플레이어 이동, 대사추가, 문 넘어가면 스테이지 이동

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
    [SerializeField] private VariableJoystick joystick;
    [SerializeField] private float moveSpeed = 3f;
    [SerializeField] private TutorialMain tutorialMain;
    private Vector3 downPosition;
    private bool isDown = false;
    private Animator anim;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        float h = this.joystick.Direction.x;
        float v = this.joystick.Direction.y;
       
        Vector3 moveDir = (Vector3.forward * v) + Vector3.right * h;
        float angle = Mathf.Atan2(moveDir.x, moveDir.z) * Mathf.Rad2Deg;
        
        //조이스틱으로 입력받아 방향 바라보게함
        if (moveDir != Vector3.zero)
        {//이동
            this.transform.Translate(moveDir.normalized * this.moveSpeed * Time.deltaTime,Space.World);
            this.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.up);//축으로 회전
         }
    }
    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"))
        {
            Debug.Log("Stage1");
            SceneManager.LoadScene("Stage1");
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TutorialMain : MonoBehaviour
{
    [SerializeField] private GameObject gateLeftGo;
    [SerializeField] private GameObject gateRightGo;
    [SerializeField] private GameObject npcGo;
    [SerializeField] private GameObject speechGo;
    private Text text;
    // Start is called before the first frame update
    void Start()
    {
        //시작시 안내 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()
    {
        
    }

    public void OpenGate()
    {
        Debug.Log("OpenGate");
        this.gateLeftGo.transform.Rotate(0,90,0);
        this.gateRightGo.transform.Rotate(0,-90, 0);
        this.text.text = "잘했어요! 문이 열렸으니 이동해볼까요?";
    }
}

 

 

 

myoskin