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

[20강] 미니게임 미션 만들기 : 전선 연결 2

서니션 2023. 1. 23. 18:31
728x90
반응형

Raycast 사용

 

오른쪽 선에 닿았는지

닿지 않았는지

if else문 작성

 

오른쪽 것들 잡아서 Box Collider 추가

2D는 RayCast가 알지 못함!

 

선이 고정되는지 확인하기

선에 가서 놓은게 아니라면 원상복귀되는지 확인

 

선이 알맞게 확인되었는지 확인해야 함

색깔을 가져오기

 

isColor를 bool[] 배열로 만들어주기

switch 문으로 작성

 

Mission6.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 Mission6 : MonoBehaviour
{
    public bool[] isColor = new bool[4];
    public RectTransform[] rights;
    public LineRenderer[] lines;
    
    Animator anim;
    PlayerCtrl playerCtrl_script;

    Vector2 clickPos;
    LineRenderer line;
    Color leftC, rightC;

    bool isDrag;
    float leftY, rightY;
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
    }

    private void Update()
    {
        // 드래그
        if (isDrag)
        {
            line.SetPosition(1, new Vector3(Input.mousePosition.x - clickPos.x, Input.mousePosition.y - clickPos.y, -10));
            // 드래그 끝
            if (Input.GetMouseButtonUp(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit hit;
                
                // 오른쪽 선에 닿았다면
                if (Physics.Raycast(ray, out hit))
                {
                    GameObject rightLine = hit.transform.gameObject;
                    
                    // 오른쪽 선 y값
                    rightY = rightLine.GetComponent<RectTransform>().anchoredPosition.y;
                    
                    // 오른쪽 선 색상
                    rightC = rightLine.GetComponent<Image>().color;
                    
                    // 선이 닿았다면 고정
                    line.SetPosition(1, new Vector3(500, rightY - leftY, -10));
                    
                    // 색 비교
                    if (leftC == rightC)
                    {
                        switch (leftY)
                        {
                            case 225 : isColor[0] = true; break;
                            case 75 : isColor[1] = true; break;
                            case -75 : isColor[2] = true; break;
                            case -225 : isColor[3] = true; break;
                        }
                    }
                    else
                    {
                        switch (leftY)
                        {
                            case 225 : isColor[0] = false; break;
                            case 75 : isColor[1] = false; break;
                            case -75 : isColor[2] = false; break;
                            case -225 : isColor[3] = false; break;
                        }
                    }
                    
                    // 성공여부 체크
                    if (isColor[0] && isColor[1] &&isColor[2] &&isColor[3])
                    {
                        Invoke("MissionSuccess",0.2f);
                    }
                }
                
                // 닿지 않았다면
                else
                {
                    line.SetPosition(1, new Vector3(0,0,-10));
                }
                isDrag = false;
            }
        }
    }

    // 미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp",true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
        
        // 초기화
        for (int i = 0; i < 4; i++)
        {
            isColor[i] = false;
            lines[i].SetPosition(1, new Vector3(0,0,-10));
        }
        
        // 랜덤
        for (int i = 0; i < rights.Length; i++)
        {
            Vector3 temp = rights[i].anchoredPosition;

            int rand = Random.Range(0, 4);
            rights[i].anchoredPosition = rights[rand].anchoredPosition;

            rights[rand].anchoredPosition = temp;
        }
    }
    
    // 엑스버튼 누르면 호출
    public void MissionCancle()
    {
        anim.SetBool("isUp",false);
        playerCtrl_script.MissionEnd();
    }
    
    // 선 누르면 호출
    public void ClickLine(LineRenderer click)
    {
        clickPos = Input.mousePosition;
        line = click;
        
        // 왼쪽 선에 y값을 넣어준 것
        leftY = click.transform.parent.GetComponent<RectTransform>().anchoredPosition.y;
        
        // 왼쪽 선 색상
        leftC = click.transform.parent.GetComponent<Image>().color;
        
        isDrag = true;
    }
    // 미션 성공하면 호출
    public void MissionSuccess()
    {
        MissionCancle();
    }
}

드디어 전설연결 끝이다

얼른 마무리 하고싶다 :ㅇ

728x90
반응형