Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomObjInstanciate : MonoBehaviour {
GameObject cubePrefab;
int batchNum = 5;
Vector3 cubeSize;
Vector3 offset;
const float min = -0.5f;
const float max = 0.5f;
void Start(){
cubeSize = gameObject.transform.localScale;
offset = gameObject.transform.localPosition;
void Update(){
void InstantiateSmallCubeAtRandom(){
// オブジェクトを立方体内のランダムな位置にインスタンス化する
for (int i = 1; i <= batchNum; i++){
float xPos = GetRandomRangeInCube() * cubeSize.x;
float yPos = GetRandomRangeInCube() * cubeSize.y;
float zPos = GetRandomRangeInCube() * cubeSize.z;
Vector3 position = new Vector3(xPos, yPos, zPos) + offset;
// Prefabをインスタンス化する
GameObject obj = Instantiate(cubePrefab, position, Quaternion.identity);
float GetRandomRangeInCube(){
float randomRange = Random.Range(min, max);
return randomRange;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.