0
votes

Currently I have code that spawns a bunch of Sphere mesh instances into the shape of a Cube (by spawning one Sphere per a Cube mesh's vertex), like this:

public CubeMesh CubeMesh;
mesh = CubeMesh.GetComponent<MeshFilter>().mesh;

public GameObject spherePrefab;
public GameObject[] spheres;

public List<TransformData> matrices1 = new List<TransformData>();
Vector3[] matrices1pos = new Vector3[24]; //cube has 24 vertices

spheres = new GameObject[mesh.vertexCount]; 
for (int i = 0; i < mesh.vertexCount; i++)
{
    matrices1pos[i] = matrices1[i].position;
    spheres[i] = Instantiate(spherePrefab, matrices1[i].position, Quaternion.identity);
}

So this works to have a bunch of sphere instances creating the shape of a Cube when you hit play.

What I'm now trying to do is move the spheres that make up this Cube shape, so that this 'Cube' shape can follow the player, without ruining the shape.

When I manipulate the Transform data of the GameObject that the above script runs on, the sphere instances don't change their position at all. They do not care what the position of the GameObject is.

I also tried parenting it to another object and moving the Parent's position, but the sphere instance positions do not care about the parent's position either.

What would be the correct approach here, for moving the 'Cube' shape around without changing the actual spheres' positions relative to one another? It looks like this (https://i.imgur.com/aGi4cOS.png), for reference.

Edit:

doing this line instead:

spheres[i] = Instantiate(spherePrefab, matrices1[i].position, Quaternion.identity, transform);

allows me to manipulate the rotation and scale of the sphere instances, but not the position

Edit2: Solved! see answer below

1
is the spherePrefab a pure model data or does it have any additional components attached? You should be able to simply spawn them all as child of one GameObject and then transform that common parent object instead of each sphere individually ... in your screenshot all sphere instances are on root level not under one parent object.derHugo

1 Answers

0
votes

I figured it out! I needed to use localPosition. Thank you! =]

spheres[i].transform.SetParent(GO2.transform);
spheres[i].transform.localPosition = Vector3.Lerp(matrices1pos[i], matrices2[i].position, t);