Item Getter
- 아이템을 획득하면 가방 UI로 이동하는 기능 구현
- Player 하위에 CircleCollider2D와 스크립트를 추가한다
- UI를 그릴 캔버스를 생성하고 Layer를 UI, Render Mode를 Screen Space - Camera로 설정
- UI만 찍을 카메라 생성해 Render Camera에 할당
- UI카메라의 Render Type을 Overlay, Projection을 Orthographic, Culling Mask에서 UI만 활성화
- 메인 카메라의 Culling Mask에서 UI만 비활성화, Stack에 UI카메라 추가
- 캔버스에 버튼을 추가하고 가방 이미지 넣기
- 아이템 오브젝트 프리팹생성
- 아이템을 획득하면 가방으로 이동하는 애니메이션을 위해 GotItem 프리팹 생성
- 파티클 시스템 생성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemGetter : MonoBehaviour
{
public RectTransform itemRectTransform;
public Canvas canvas;
public GameObject itemPrefab;
public GameObject getEffectPrefab;
public Camera camera;
private float EaseInOut(float t)
{
return t < 0.5f ? 2f * t * t : -1f + (4f - 2f * t) * t;
}
IEnumerator GoingTBox(RectTransform itemTransform, RectTransform boxTransform)
{
float duration = 2.0f;
float t = 0.0f;
Vector3 itemBeginPOS = itemTransform.position;
while (1.0f >= t / duration)
{
Vector3 newPosition = Vector3.Lerp(itemBeginPOS,
boxTransform.position, EaseInOut(t / duration));
itemTransform.position = newPosition;
t += Time.deltaTime;
yield return null;
}
itemTransform.position = boxTransform.position;
Destroy(itemTransform.gameObject);
var particle = Instantiate(getEffectPrefab, boxTransform.position,getEffectPrefab.transform.rotation);
var vector3 = particle.transform.position;
vector3.z = 0.0f;
particle.transform.position = vector3;
Destroy(particle, 3.0f);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other);
var newObject = Instantiate(itemPrefab, other.transform.position, Quaternion.identity, canvas.transform);
newObject.GetComponent<Image>().sprite = other.GetComponent<SpriteRenderer>().sprite;
newObject.transform.position = other.transform.position;
var newScreenPosition = Camera.main.WorldToScreenPoint(newObject.transform.position);
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), newScreenPosition, camera, out localPoint);
newObject.transform.localPosition = localPoint;
StartCoroutine(GoingTBox(newObject.GetComponent<RectTransform>(), itemRectTransform));
Destroy(other.gameObject);
}
}
- ItemGetter에 스크립트와 오브젝트 할당
'TIL' 카테고리의 다른 글
[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 21-22일차 2D게임 제작(4) (1) | 2024.12.19 |
---|---|
[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 20일차 2D게임 제작(3) (0) | 2024.12.17 |
[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 18일차 2D 게임 제작 (0) | 2024.12.14 |
[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 17일차 유니티 활용 (0) | 2024.12.12 |
[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 16일차 알고리즘(정렬) (0) | 2024.12.11 |