- Home /
Question by
Captaion · Jun 12, 2020 at 07:27 PM ·
gameobjectinstantiategenerationcoverspacing
Generate objects around object based on bounding size
Basically I'm trying to evenly generate and space objects around the "cover" object based on its bounding size and I've got it for whole numbers but I can't quite figure out why I can't get it to work for a size with a decimal point.
Any advice would be really appreciated, and I might be overlooking something obvious but I feel like I'm stuck, for the code below I used a standard 1x1x1 cube in the teeest variable, and the object you place the component on should be a standard cube 2x1x2 or bigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cover : MonoBehaviour
{
public GameObject teeest;
public Collider coverCollider;
public MeshRenderer rend;
public List<CoverNode> nodes;
public List<GameObject> testList;
public int numberOfNodes, nodesX, nodesZ, iterator, nodesInSet;
public float spacingX, spacingZ, currentSpacing, distance, picketWidth;
public string coverHeight, coverType;
public Vector3 nodePos, oppositePos;
private void Start()
{
coverCollider = GetComponent<Collider>();
rend = GetComponent<MeshRenderer>();
GenerateNodes();
}
public void GenerateNodes()
{
//This controls the switch below
int mode = 0;
//Round down the bounds of the renderer to get the nodes per side
nodesX = Mathf.FloorToInt(rend.bounds.size.x);
nodesZ = Mathf.FloorToInt(rend.bounds.size.z);
//Spacing Calculation
spacingX = (rend.bounds.size.x - (picketWidth * nodesX)) / (nodesX * 0.5f);
spacingZ = (rend.bounds.size.z - (picketWidth * nodesZ)) / (nodesZ * 0.5f);
//Number of nodes is the total generated
numberOfNodes = ((nodesX + nodesZ) * 2);
//Iterator is half of the total number as we do two sides at once
iterator = numberOfNodes / 2;
for (int i = 0; i < iterator; i++)
{
switch (mode)
{
//x
case 0:
if (currentSpacing == 0)
{
currentSpacing = spacingX;
}
else
{
currentSpacing += spacingX;
}
distance = rend.bounds.extents.z + picketWidth;
nodePos = rend.bounds.center + (transform.forward * distance) + (transform.right * (distance - currentSpacing));
oppositePos = rend.bounds.center + (-transform.forward * distance) + (transform.right * (distance - currentSpacing));
nodesInSet++;
if (nodesInSet == nodesX)
{
mode = 1;
currentSpacing = 0;
nodesInSet = 0;
}
break;
//z
case 1:
if (currentSpacing == 0)
{
currentSpacing = spacingZ;
}
else
{
currentSpacing += spacingZ;
}
distance = rend.bounds.extents.x + picketWidth;
nodePos = rend.bounds.center + (transform.right * distance) + (transform.forward * (distance - currentSpacing));
oppositePos = rend.bounds.center + (-transform.right * distance) + (transform.forward * (distance - currentSpacing));
nodesInSet++;
if (nodesInSet == nodesZ)
{
currentSpacing = 0;
nodesInSet = 0;
}
break;
}
GameObject node1 = Instantiate(teeest, nodePos, transform.rotation);
GameObject node2 = Instantiate(teeest, oppositePos, transform.rotation);
testList.Add(node1);
testList.Add(node2);
}
}
}
Comment