using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDown_CreatePrefabs : MonoBehaviour
{
// 마우스 클릭하면 그 자리에 프리팹 생성
// 마우스 클릭한 위치보다 조금 앞쪽에 프리팹 나타나게
public GameObject newPrefab; // 프리팹(게임오브젝트) 저장 - ghost
Vector3 pos;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// 마우스 클릭한 위치를 카메라세상안에서의 위치로 변환해서
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = -5;
// 그 위치에 새로운 프리팹 등장시킨다.
GameObject newGhost = Instantiate(newPrefab);
newGhost.transform.position = pos;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SomeTimes_RandomPrefabs : MonoBehaviour
{
public GameObject newPrefab; // 프리팹 저장(햄버거)
float intervalSec = 1; // 나타나는 간격(1초)
void Start()
{
InvokeRepeating("CreatePrefab", intervalSec, intervalSec);
}
void CreatePrefab()
{
// 구름의 범위(area)내에서 랜덤으로 나타나게
Vector3 area = GetComponent<SpriteRenderer>().bounds.size; // 구름의 이미지 범위
Vector3 pos = this.transform.position; // 구름의 위치값
// 랜덤으로 나타나게
pos.x += Random.Range(-area.x/2, area.x/2);
pos.y += Random.Range(-area.y/2, area.y/2);
// pos.z = -5;
GameObject newHam = Instantiate(newPrefab);
newHam.transform.position = pos;
}
}
'----------고1---------- > 게임엔진' 카테고리의 다른 글
[고1 게임엔진] 11 - (1) (0) | 2023.11.13 |
---|---|
[고1 게임엔진] 10 - (3) (0) | 2023.10.18 |
[고1 게임엔진] 10 - (1) (0) | 2023.10.11 |
[고1 게임엔진] 09 - (6) (0) | 2023.09.25 |
[고1 게임엔진] 09 - (5) (2) | 2023.09.20 |