Unity/유니티 기본
[4.7] 떨어지는 거 바구니로 받는 게임
ljw4104
2021. 4. 7. 18:12
과정
- 클릭하는 칸으로 바구니 이동
- 과일 또는 폭탄이 랜덤으로 위에서 떨어짐
- UI 표시 및 30초가 지나면 아무것도 떨어지지 않음.
- 레벨 디자인
1. 클릭하는 칸으로 바구니 이동
- ScreenPointToRay(Input.mousePosition) 메서드를 통해 카메라 좌표에서 게임화면 안쪽 방향으로 향해 나아가는 광선(Ray)를 구한다.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- ScreenPointToRay를 통해 구한 광선은 콜라이더와의 충돌을 감지할 수 있다 .
- Physis.Raycast라는 메서드를 통해 콜라이더와의 충돌이 있었는지? 있었으면 어떤 콜라이더와의 충돌이 있었는지에 대한 정보를 저장한다.
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,z);
}
* 콜라이더와의 움직임을 체크하기 때문에 바닥에 콜라이더가 없으면 동작하지 않는다.
2. 과일 또는 폭탄이 랜덤으로 떨어짐
- 과일과 폭탄은 무한으로 생성되어야하기 때문에 Prefab으로 하는편이 편하다.
this.delta += Time.deltaTime;
if (this.delta > this.span)
{
this.delta = 0;
GameObject item;
int dice = Random.Range(1, 11);
if (dice <= this.ratio)
{
item = Instantiate<GameObject>(bombPrefab);
}
else
{
item = Instantiate<GameObject>(applePrefab);
}
float x = Random.Range(-1, 2);
float z = Random.Range(-1, 2);
item.transform.position = new Vector3(x, 4, z);
item.GetComponent<ItemController>().dropSpeed = this.speed;
using UnityEngine;
public class ItemController : MonoBehaviour
{
public float dropSpeed = -0.03f;
// Update is called once per frame
void Update()
{
transform.Translate(0, this.dropSpeed, 0);
if(transform.position.y < -1.0f)
{
Destroy(gameObject);
}
}
}
3. UI 제작
[4.7] UI 제작 시 기본 셋팅
1. UI 전용 카메라 만들기 UI 패널만 찍는 카메라를 하나 만들어서 관리하기 편하게 한다. Tag를 UICamera, Layer을 UI. Camera Component에서 여러 속성들을 변경해주어야 한다. 속성 값 Clear Flags Depth only..
silimy5465.tistory.com
- 세팅 후 다른과정은 똑같다.