Unity/베어유 : 어몽어스 개발 노트

[18강] 미니게임 미션 만들기 : 각도 조종하기

서니션 2023. 1. 20. 20:58
728x90
반응형

핸들 앞에 콜라이더 수정하기

Mission5를 만들어 주고 위치 수정해주고

핸들을 다른 ui로 바꿔준다

ui - image로 프로펠러를 만들어준다

Rotate 만들어서 색깔 바꿔주기

 

스크립트 작성

trash -> handle로 바꾸기

쓸모 없는 부분 삭제

 

x 부분에 핸들이 이동할 수 있는 범위 작성

rect_handle.anchoredPosition =
    new Vector2(184, Mathf.Clamp(rect_handle.anchoredPosition.y, -195, 195));

play해서 잘 작동되는지 확인

 

z축을 움직여주기 위해 (프로펠러를 움직여주기 위해)

 

rotate.eulerAngles = new Vector3(0, 0, 90 * rect_handle.anchoredPosition.y / 195);

update 문 안에 작성

 

핸들 움직이면 프로펠러의 위치 변하는 것 확인 가능!

 

핸들의 위치도 랜덤으로 만들어주기 위해 random 기능 작성

 

Mission5를 Prop_Down으로 옮기고 이걸 복제해서 Prop_Up에도 넣어준다

그리고 위에거는 콜라이더를 좀 조정해준다

 

Mission5.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
using Image = UnityEngine.UI.Image;
using Random = UnityEngine.Random;

public class Mission5 : MonoBehaviour
{
    public Transform rotate, handle;
    public Color blue, red;
    
    Animator anim;
    PlayerCtrl playerCtrl_script;
    RectTransform rect_handle;
    
    bool isDrag, isPlay;
    float rand;
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        rect_handle = handle.GetComponent<RectTransform>();
    }

    private void Update()
    {
        if(isPlay)
        {
            // 드래그
            if (isDrag)
            {
                handle.position = Input.mousePosition;
                rect_handle.anchoredPosition =
                    new Vector2(184, Mathf.Clamp(rect_handle.anchoredPosition.y, -195, 195));
                
                // 드래그 끝
                if (Input.GetMouseButtonUp(0))
                {
                    // 성공여부 체크
                    if (rect_handle.anchoredPosition.y > -5 && rect_handle.anchoredPosition.y < 5)
                    {
                        Invoke("MissionSuccess",0.2f);
                        isPlay = true;
                    }
                    isDrag = false;
                }
            }

            rotate.eulerAngles = new Vector3(0, 0, 90 * rect_handle.anchoredPosition.y / 195);
            
            // 색 변경
            if (rect_handle.anchoredPosition.y > -5 && rect_handle.anchoredPosition.y < 5)
            {
                rotate.GetComponent<Image>().color = blue;
            }
            else
            {
                rotate.GetComponent<Image>().color = red;
            }
        }
    }

    // 미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp",true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
        
        // 초기화
        rand = 0;
        
        // 랜덤
        rand = Random.Range(-195, 195);
        
        // 예외처리
        while (rand >= -10)
        {
            rand = Random.Range(-195, 195);
        }
        rect_handle.anchoredPosition = new Vector2(184, rand);
        
        isPlay = true;
    }
    
    // 엑스버튼 누르면 호출
    public void MissionCancle()
    {
        anim.SetBool("isUp",false);
        playerCtrl_script.MissionEnd();
    }
    
    // 손잡이 누르면 호출
    public void ClickHandle()
    {
        isDrag = true;
    }
    // 미션 성공하면 호출
    public void MissionSuccess()
    {
        MissionCancle();
    }
}

 

기능 실행 영상

 


새로운 핸들 미션을 만들어보았다.

스크립트 이해가 잘 되었다.

728x90
반응형