초밥이미지를 프리팹화 시킨 후 특정 키를 눌렀을때 랜덤(Random.Range)으로 생성(스폰)하는 방법과 프리팹 생성방법과 오브젝트끼리 충돌했을 때 파괴하는 방법을 알려드립니다.
#랜덤생성 #프리팹충돌시파괴 #InstantiateDestroy
[RandomSpawn.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn : MonoBehaviour
{
[SerializeField]
GameObject sushi1, sushi2, sushi3, sushi4, sushi5, sushi6;
int SpawnObj;
void Update()
{
SpwanPlay();
}
void SpwanPlay()
{
bool keydown = Input.GetKeyDown(KeyCode.Space);
if (keydown)
{
SpawnObj = Random.Range(1, 7);
switch (SpawnObj)
{
case 1:
Instantiate(sushi1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(sushi2, transform.position, Quaternion.identity);
break;
case 3:
Instantiate(sushi3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(sushi4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(sushi5, transform.position, Quaternion.identity);
break;
case 6:
Instantiate(sushi6, transform.position, Quaternion.identity);
break;
}
}
}
}
[DestroyZone.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyZone : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
}
}
}
댓글