Instantiate right next to each other overlap problem

I’m having trouble figuring out where i’m going wrong. I’m using a mesh.bound to find the size of the next and previous mesh in the queue and using the extent.x and the localscale.x to figure out how far over I would need to move in the x so that they wouldn’t overlap but they still do. In my head it should work lol. What am I overlooking? :frowning:

I’ve attached a pic to help explain what i’m trying to achieve.
Any help would be appreciated.
Thanks


void CreateStartingBlock()
	{
		
		for(fillBlock = 0.0f; fillBlock < blockSize;)
		{
			rnd = Random.Range(1,17);
			o = (Transform)Instantiate(houses[rnd], transform.position, transform.rotation);

			objectQueue.Enqueue(o);
			objectList.Add (o);
			
			Mesh oMesh = o.GetComponent<MeshFilter>().mesh;
			Bounds oBounds = oMesh.bounds;

			int oldIndex = objectList.Count - 1;

			Transform oldTransform = objectList[oldIndex];
			Mesh nMesh = oldTransform.GetComponent<MeshFilter>().mesh;
			Bounds nBounds = nMesh.bounds;


			//o.localPosition = nextPosition;
			nextPosition.x += (oldTransform.localScale.x / 2) + (o.localScale.x / 2) + oBounds.extents.x + nBounds.extents.x;
			o.localPosition = nextPosition;
			
			fillBlock += o.localScale.x + oBounds.size.x;

		}

		blockComplete = true;	
		ResetFillBlock();
	}

1 Answer

1

nextPosition.x += (oldTransform.localScale.x / 2) + (o.localScale.x / 2) + oBounds.extents.x + nBounds.extents.x;

Note that mesh.bounds is in local coordinates, so it needs to be multiplied by localScale. In addition, localScale added here is meaningless without knowing the size of the mesh used.

Thanks, I just had to multiply it O_o...Now I just have to fix the loop because it holding on to the wrong Bounds at some point.