[Unity3D] GetComponent().bounds.sizeを使ってオブジェクトを隙間なく配置する方法が知りたい

質問内容

提示コードなのですが乱数で出現するオブジェクトを隙間なく配置したいのですがうまくできませんこれはなぜでしょうか?

実現したいこと

乱数で出現するオブジェクトを隙間なく敷き詰めて配置したい

試したこと

提示のコードのコメント部内部でbounds.sizeがオブジェクトの幅であることを確認提示画像A
提示のコードのコメント部内部でbounds.size / 2 がオブジェクトの幅の半分であることを確認提示画像B
提示コードはオブジェクトの半分のサイズを追加して、その後、次のオブジェクトのサイズの半分を足して前のオブジェクトの隣にピッタリ描画するというコードですが。なぜか提示画像Bのような現状が起きます。

提示コード

cs

1using System.Collections;2using System.Collections.Generic;3using System.Transactions;4using UnityEngine;5 6public class StageManager : MonoBehaviour7{8 [SerializeField] private List<GameObject> list = new List<GameObject>();9 10 // Start is called before the first frame update11 void Start()12 {13 Generate(); 14 15 }16 17 // Update is called once per frame18 void Update()19 {20 21 }22 void Generate()23 {24 Vector3 currentPosition = transform.position;25 26 for (int i = 0; i < 10; i++)27 {28 int random = Random.Range(0, list.Count);29 30 GameObject go = Instantiate(list[0], transform);31 32 Debug.Log(go.GetComponent<Renderer>().bounds.size / 2); 3334 Vector3 pos = currentPosition + (go.GetComponent<Renderer>().bounds.size / 2); 35 // Vector3 pos = currentPosition;3637 38 pos.x = 0;39 pos.y = 0;40 go.transform.position = pos;41 42 4344 currentPosition += go.GetComponent<Renderer>().bounds.size / 2;45 //currentPosition += go.GetComponent<Renderer>().bounds.size;4647 Debug.Log(go.GetComponent<Renderer>().bounds.size / 2); 48 } 49 }50}

参考画像

提示コードBは検証のためオブジェクトをSceneでずらしています。
またオブジェクトはじゃっかん屋根やあるためすこし空白があります

A

イメージ説明

B

イメージ説明

コメントを投稿

0 コメント