6- 두더지 잡기 툴 게임 만들기(3D)
완성 샷
어느 정도 툴을 알겠고 C#에 대해서 공부가 좀 필요하다는 걸 깨달음
미리 받아놓은 3D 모델링을 배치해 주세요
사과 -1 3 0
폭탄 1 3 0
타일맵, 바구니 0 0 0
햇빛 0 3 0 (너무 강하니까 Intensity를 0.7로 설정)
메인 카메라 0 3.8 -1.6
그리고
캔버스를 하나 만들어 주고
Time point 를 이름 지어주고
위치는 오른쪽 상단에 하고
time -60 -25
160 40 입력
point -65 -70
160 40 입력
이쁘장하게 ui나왔네요
BasketController 넣어주세요
바구니 컨트롤
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasketController : MonoBehaviour
{
public AudioClip appleSE;
public AudioClip bombSE;
AudioSource aud; //총 관리 소스
GameObject director;
void Start()
{
this.director = GameObject.Find("GameDirector");
this.aud = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other) //충돌 시작 지점일때
{
if (other.gameObject.tag == "Apple") //사과라는 태그가 들어왔을때
{
this.director.GetComponent<GameDirector>().GetApple();
//GameDirector안에있는 getapple함수를 가져온다.
this.aud.PlayOneShot(this.appleSE);
//오디오 소스 aud를 동시에 관리하기 위해서 shot 이용
}
else
{
this.director.GetComponent<GameDirector>().GetBomb();
this.aud.PlayOneShot(this.bombSE);
}
Destroy(other.gameObject);
// 안맞고 혼자 떨어지면 없어짐
}
void Update()
{
if (Input.GetMouseButtonDown(0)) //마우스를 눌렀을때
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// 카메라 기준으로 마우스를 누른 위치가 ray에 저장됨
RaycastHit Hit; //레이캐스트에서 정보를 다시 가져오는 데 사용되는 구조
if (Physics.Raycast(ray,out Hit, Mathf.Infinity))
{//광선이 부딪히면 히트포인트의 x및 z좌표를 정수로 반올림시킴
float x = Mathf.RoundToInt(Hit.point.x);
float z = Mathf.RoundToInt(Hit.point.z);
transform.position = new Vector3(x, 0.0f, z);
//y제외하고 레이를 이용해 히트 포인트로 이동함.
}
}
}
}
https://docs.unity3d.com/ScriptReference/RaycastHit.html
RaycasHit를 쓰고 쓸 수 있는 속성에 대한 글
Raycast에 대한 속성
Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
(레이의 시작위치 // 레이의 방향 // 레이가 충돌을 검출할 수 있는 최대거리, 레이 충돌 검사할 레이어 마스크,
레이캐스트에서 트리거에 대한 검사 방법)
itemController
빈 오브젝트에 한개 만들어주세요
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class itemController : MonoBehaviour
{
public float dropSpeed = -0.03f;
void Update()
{
transform.Translate(0, this.dropSpeed, 0);
// y축이-0.03의 속도로 떨어짐 프레임마다
if (transform.position.y < -1.0f)// -1.0의 위치를 y가 넘어서면
{
Destroy(gameObject); // 없어짐
}
}
}
itemGenerator 아이템 감독
빈오브젝트에 넣어 주도록 하자
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class itemGenerator : MonoBehaviour
{
public GameObject applePrefab; //사과를 넣을 prefab
public GameObject bombprefab; //폭탄을 넣을 prefab
float span = 1.0f; //시간격차
float delta = 0;
int ratio = 2; //랜덤 확률을 구하기위해서
float speed = -0.03f;
public void SetParameter(float span,float speed,int ratio)
{
this.span = span;
this.speed = speed;
this.ratio = ratio;
}//객체지향으로 따로 하나하나 선언해줌 나중에 레벨을 만들기위해서 선언해주는것임
void Update()
{
this.delta += Time.deltaTime; //만약 프레임이 가면
if (this.delta > this.span)
{ //프레임 간의 격차가 1초이상일떄 결론은 1초뒤에 떨어지는거
this.delta = 0; //초기화 해줘야됨
GameObject item;
int dice=Random.Range(1, 11); //랜덤 함수 1~11까지
if (dice <= this.ratio) // ratio를 2라고 치니까 폭탄이 나올 확률을 알수있다.
{
item = Instantiate(bombprefab) as GameObject;
}
else
{
item = Instantiate(applePrefab) as GameObject;
}
float x = Random.Range(-1, 2); //랜덤함수 x축
float z = Random.Range(-1, 2); //랜덤함수 z축
item.transform.position = new Vector3(x, 4, z);
item.GetComponent<itemController>().dropSpeed= this.speed;
// itemController에있는 dropSpeed를 가져와서 이걸 떨어지는 사과와 폭탄의 스피드를 결정하게 됨
}
}
}
코드를 넣고 프리팹도 두 개다 넣어줘야 된다!
GameDirector 게임 감독이다.
빈오브젝트에 넣어주도록 하자
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject timerText;
GameObject pointText;
int point = 0;
float time = 30.0f;
GameObject generator;
public void GetApple()
{
this.point += 100;
} //100점 추가
public void GetBomb()
{
this.point /= 2;
} //폭탄먹으면 /2
void Start()
{
this.timerText = GameObject.Find("Time");
this.pointText = GameObject.Find("Point");
this.generator = GameObject.Find("itemGenerator");
} //text를 연결하기 위해서 그리고 itemGenerator와 연결
void Update()
{ // 시간마다 레벨이 달라짐
if (this.time < 0)
{
this.time = 0;
this.generator.GetComponent<itemGenerator>().SetParameter(10000.0f, 0, 0);
}else if(0<=this.time && this.time < 5)
{
this.generator.GetComponent<itemGenerator>().SetParameter(0.9f, -0.04f, 3);
}
else if (5 <= this.time && this.time < 10)
{
this.generator.GetComponent<itemGenerator>().SetParameter(0.4f, -0.06f, 6);
}
else if (10 <= this.time && this.time < 20)
{
this.generator.GetComponent<itemGenerator>().SetParameter(0.7f, -0.04f, 4);
}
else if (20 <= this.time && this.time < 30)
{
this.generator.GetComponent<itemGenerator>().SetParameter(1.0f, -0.03f, 2);
}
this.time-= Time.deltaTime; //프레임 마다 시간 줄어듬
this.timerText.GetComponent<Text>().text=this.time.ToString("F1"); //한자리까지 나옴
this.pointText.GetComponent<Text>().text = this.point.ToString() + "point";
// 점수와 point 실시간으로 업데이트
}
}
느낀점 - 책 한 권을 다 끝내고 나니
3d 개념이 너무 내가 부족하다고 느낀다.
그리고 게임을 만들기 위해서는
기획이 가장 중요하다는 걸 알 거 같다.
일단 뼈대를 다 만들어 넣고 스크립트를 만드는 게 가장 좋지 않을까 생각한다.