728x90
728x90
이제 이어주면 멈춰야 한다!
오른쪽의 4개 선에 박스 콜라이더를 추가한다.
이제 콜라이더 크기를 위와 같이 해주면 이 박스를 감지하게 된다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Mission6 : MonoBehaviour
{
Animator anim;
PlayerCtrl playerCtrl_script;
Vector2 clickPos;
LineRenderer line;
float leftY, rightY;
bool isDrag;
// Start is called before the first frame update
void Start()
{
//getcomponent를 그냥 쓰면 안된다. 여기서 애니메이션은 하위 파일에 있다.
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;
rightY = rightLine.GetComponent<RectTransform>().anchoredPosition.y; //오른쪽 선 y값
//이어주기
line.SetPosition(1, new Vector3(500,rightY-leftY , -10));
}
else
{
// 닿지 않았다면
line.SetPosition(1, new Vector3(0, 0, -10));
}
isDrag = false;
}
}
}
//미션 시작
public void MissionStart()
{
anim.SetBool("IsUp", true);
playerCtrl_script = FindObjectOfType<PlayerCtrl>();
}
//x버튼 누르면 호출
public void ClickCancle()
{
anim.SetBool("IsUp", false);
playerCtrl_script.MissionEnd();
}
//선 누르면 호출
public void ClickLine(LineRenderer click)
{
clickPos = Input.mousePosition;
line = click;
leftY = click.transform.parent.GetComponent<RectTransform>().anchoredPosition.y;//왼쪽 선 y값
isDrag = true;
}
// 미션 성공하면 호출될 함수
public void MissionSuccess()
{
ClickCancle();
}
}
코드는 잘 작동하는데 길이가 절반인 것은 아직 잘 모르겠따...
알맞게 이어진건지 판단하는 것이 필요하다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
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;
float leftY, rightY;
bool isDrag;
Color leftC, rightC;
// Start is called before the first frame update
void Start()
{
//getcomponent를 그냥 쓰면 안된다. 여기서 애니메이션은 하위 파일에 있다.
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;
rightY = rightLine.GetComponent<RectTransform>().anchoredPosition.y; //오른쪽 선 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;
}
}
//x버튼 누르면 호출
public void ClickCancle()
{
anim.SetBool("IsUp", false);
playerCtrl_script.MissionEnd();
}
//선 누르면 호출
public void ClickLine(LineRenderer click)
{
clickPos = Input.mousePosition;
line = click;
leftY = click.transform.parent.GetComponent<RectTransform>().anchoredPosition.y;//왼쪽 선 y값
isDrag = true;
//왼쪽 선 색상
leftC = click.transform.parent.GetComponent<Image>().color;
}
// 미션 성공하면 호출될 함수
public void MissionSuccess()
{
ClickCancle();
}
}
line.SetPosition(1,new Vector3((Input.mousePosition.x- clickPos.x)*2, (Input.mousePosition.y - clickPos.y)*2,-10));
마우스 따라오는 것을 이렇게 바꾸면 되네요ㅣ... 왠지는 물어봐야 겠습니다.
728x90
'개발 > 게임 교육' 카테고리의 다른 글
[Unity class] 게임 교육 22 - 킬 퀘스트 준비 (0) | 2024.01.06 |
---|---|
[Unity class] 게임 교육 21 - 미션 게이지바 만들기 (2) | 2024.01.06 |
[Unity class] 게임 교육 19 - 마지막 미션1 (0) | 2024.01.06 |
[Unity class] 게임 교육 18 - 각도 조종하기 (1) | 2024.01.06 |
[Unity class]게임 교육 17 - 미션 4 (1) | 2024.01.06 |