<구름>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sometimes_RandomPrefab : MonoBehaviour
{
public GameObject newPrefab; // 만드는 프리팹 :Inspector에 지정한다
public float intervalSec = 1; // 작성 간격(초):Inspector로에 지정한다
void Start()
{ // 처음에 시행한다
// 지정 초 수마다 CreatePrefab를 반복 실행하는 예약
InvokeRepeating("CreatePrefab", intervalSec, intervalSec);
}
void CreatePrefab()
{
// 이 오브젝트의 범위 내에 랜덤으로
Vector3 area = GetComponent<SpriteRenderer>().bounds.size;
Vector3 newPos = this.transform.position;
newPos.x += Random.Range(-area.x / 2, area.x / 2);
newPos.y += Random.Range(-area.y / 2, area.y / 2);
newPos.z = -5; // 앞 쪽에 표시
// 프리팹을 만든다
GameObject newGameObject = Instantiate(newPrefab) as GameObject;
newGameObject.transform.position = newPos;
}
}
---------------------------------------------------------------------
<폭탄>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Forever_MoveV : MonoBehaviour
{
public float speed = -5;
void FixedUpdate()
{
this.transform.Translate(0, speed / 50, 0); // 수직 이동한다
}
}
---------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy_Prefab : MonoBehaviour
{
// 충돌하면 게임을 정지한다
public string targetObjectName; // 목표 오브젝트 이름 : Inspector에 지정
public string showObjectName; // 표시 오브젝트 이름 : Inspector에 지정
GameObject showObject;
public float limitSec = 3; // 초 수 : Inspector에 지정
void Start()
{ // 처음에 시행한다
// 지우기 전에 표시 오브젝트를 기억해 둔다
showObject = GameObject.Find(showObjectName);
Time.timeScale = 1; // 시간을 움직인다
Destroy(this.gameObject, limitSec); // 지정 초 후에 소멸하는 예약
}
void OnCollisionEnter2D(Collision2D collision) // 충돌했을 때
{
// 만약 충돌한 것의 이름이 목표 오브젝트였다면
if (collision.gameObject.name == targetObjectName)
{
showObject.SetActive(true); // 지웠던 것을 표시한다
// 시간을 멈춘다
Time.timeScale = 0;
}
}
}