Hi everyone!
For a class project, I’m recreating a Motus Art animation (which was coded using p5.js) into unity.
The animation in question is this one, and I’ve mostly managed to recreate spawning first the boxes through the Start function, putting them into a dictionary, and then updating their height into the Update function.
The issue I’m having is that it seems that the cubes are not updated independently, but rather animate in groups, not creating the effect that I’m looking for.
In javascript, the code for creating and animating these cubes is:
for (let x = 0; x < boxCount; x++) {
const xThea = sin((timer * TWO_PI * 8) + (x / 3));
for (let y = 0; y < boxCount; y++) {
const yThea = cos((timer * TWO_PI * 4) + (y / 10));
const h = map(xThea * yThea, -1, 1, 3, 8);
push();
translate(x * boxSize, y * boxSize, boxSize * h * 0.5);
box(boxSize, boxSize, h * boxSize);
pop();
}
}
timer += speed;
if (timer >= 1) {
timer = 0;
}
and is all in the Update function of p5. I didn’t know of a way to constantly delete and create the cubes every single frame, so I’ve created the cube in the Start function:
for (int x = 0; x < boxCount; x++) {
float xThea = Mathf.Sin((timer * Mathf.PI * 16) + (x / 3));
for (int y = 0; y < boxCount; y++) {
float yThea = Mathf.Cos((timer * Mathf.PI * 8) + (y / 10));
float h = MapValue(-1f, 1f, 3f, 8f, xThea * yThea);
string loc = x.ToString() + "_" + y.ToString();
GameObject cubeObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubeObject.transform.localScale = new Vector3(boxSize, boxSize, h * boxSize);
cubeObject.transform.localPosition = new Vector3(x * boxSize, y * boxSize, boxSize * h * 0.5f);
cubes.Add(loc, cubeObject);
}
}
timer += speed;
if (timer >= 1) {
timer = 0;
}
and then I update all the cubes with the update function:
for (int x = 0; x < boxCount; x++) {
float xThea = Mathf.Sin((timer * Mathf.PI * 16) + (x / 3));
for (int y = 0; y < boxCount; y++) {
float yThea = Mathf.Cos((timer * Mathf.PI * 8) + (y / 10));
float h = MapValue(-1, 1, 3, 8, xThea * yThea);
GameObject cubeObject = cubes[x.ToString() + "_" + y.ToString()];
cubeObject.transform.localScale = new Vector3(boxSize, h * boxSize, boxSize);
cubeObject.transform.localPosition = new Vector3(x * boxSize, boxSize * h * 0.5f, y * boxSize);
}
}
timer += speed;
if (timer >= 1) {
timer = 0;
}
And this is the result I get:
I think that the issue concerns the dictionary, as maybe it updates several cubes at once, but I honestly don’t know how to approach it.
Can someone please help me understand this issue?
Thank you very much in advance <3