Hi I’m struggling to write the code for a maths problem in displaying an output of the picture where the cube has the inside invisible and only the corners displaying with specific colour alignments with the hint according to my professor it should be the inverse of this example code here but I tried and tested everything please could someone help me understand how to write code in update cube thanks
image output i should have
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class SpawnScript : MonoBehaviour
{
private const int RESOLUTION = 11; // Number of spheres per side.
private const int EXTENT = 5; // Cube size: half of size length.
public float stopAfterSecs = 0;
public GameObject spherePrefab;
readonly Func<float, float, float, float, float, float> map =
(v, from1, to1, from2, to2) =>
Mathf.Lerp(from2, to2, Mathf.InverseLerp(from1, to1, v));
private GameObject[,,] cube1, cube2, cube3, cube4;
private GameObject[,,] CreateCube()
{
GameObject[,,] result = new GameObject[RESOLUTION, RESOLUTION, RESOLUTION];
for (int x = 0; x < RESOLUTION; x++)
{
for (int y = 0; y < RESOLUTION; y++)
{
for (int z = 0; z < RESOLUTION; z++)
{
result[x, y, z] =
Instantiate(spherePrefab,
Vector3.zero,
spherePrefab.transform.rotation);
}
}
}
return result;
}
private void UpdateCube(GameObject[,,] cube,
float xCentre,
float yCentre,
Func<Vector4, Vector4> colour,
Func<Vector4, Vector4> size,
bool alterXPos = false)
{
float t = map(Time.time % 10f, 0, 10, -1, 1);
for (int x = 0; x < RESOLUTION; x++)
{
for (int y = 0; y < RESOLUTION; y++)
{
for (int z = 0; z < RESOLUTION; z++)
{
float normalX = map(x, 0, RESOLUTION - 1, -1, 1);
float normalY = map(y, 0, RESOLUTION - 1, -1, 1);
float normalZ = map(z, 0, RESOLUTION - 1, -1, 1);
float xPos = map(normalX, -1, 1, -EXTENT, EXTENT);
float yPos = map(normalY, -1, 1, -EXTENT, EXTENT);
float zPos = map(normalZ, -1, 1, -EXTENT, EXTENT);
// Alter x position based on y and z coordinates
// Alter x position based on y and z coordinates
if (cube == cube1)
{
Vector4 pos = new Vector4(xPos, yPos, zPos, t);
pos.x += (pos.y + pos.z) * 0.5f;
xPos = pos.x; // Update xPos with the altered value
}
GameObject obj = cube[x, y, z];
obj.transform.position = new Vector3(xCentre + xPos, yCentre + yPos, zPos);
Vector4 thisSize = size(new Vector4(normalX, normalY, normalZ, t));
obj.transform.localScale = new Vector3(map(thisSize.x, -1, 1, 0, 1),
map(thisSize.y, -1, 1, 0, 1),
map(thisSize.z, -1, 1, 0, 1));
Renderer r = obj.GetComponent<Renderer>();
Vector4 v = colour(new Vector4(normalX, normalY, normalZ, t));
r.material.color = new Color(map(v.x, -1, 1, 0, 1),
map(v.y, -1, 1, 0, 1),
map(v.z, -1, 1, 0, 1),
map(v.w, -1, 1, 0, 1));
}
}
}
}
void Start()
{
cube1 = CreateCube();
cube2 = CreateCube();
cube3 = CreateCube();
cube4 = CreateCube();
}
void Update()
{
if (Time.time <= stopAfterSecs)
{ UpdateCube(-20, +20,
colour: (pos) => {
if (pos.z + pos.y > 0.33) {
return new Vector3(1, 0, -1);
} else if (pos.x > -0.33) {
return new Vector3(1, -1, -1);
} else {
return new Vector3(-1, -1, 1);
}
},
size: (pos) => {
if (pos.magnitude >= 1) {
float f = Random.Range(-1f, 1f);
return new Vector3(f, Random.Range(-1f, 1f), f);
} else {
return new Vector3(-1, -1, -1);
}
}
);
I tried changing the magnitude and even the numbers its so hard hope u guys can help me it will be appreciated!!!
amishi sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.