Hello i am having some trouble linking the vertices(Vector3) to the RedSpheres(Vector3) so i can manipulate the vertices,
to see what i am talking about see Pic1.
And in Pic2 are the results i am getting, does anyone know how to fix this?
-Pic1 (What i am trying to achieve)
[75897-capture14.png*_|75897]
-Pic2 (The results i am getting when i move the Cube and its Spheres)
[75900-capture13.png*_|75900]
And here is the code so you know what i have done.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralRoad : MonoBehaviour
{
private Vector3[] cubePoints;
private Vector3[] spherePoints;
public GameObject cubePrefab;
public GameObject spherePrefab;
private Vector3[] vertices;
private Mesh mesh;
public int cubeAmount, sphereAmount, offset;
//private int xSize, zSize;
private Vector3 gizmoCubeSize = new Vector3(0.1f, 0.1f, 0.1f);
void Start()
{
//xSize = cubeAmount;
//zSize = sphereAmount;
cubePoints = new Vector3[(cubeAmount + 1)];
for (int i = 0, x = 0; x <= cubeAmount; i++, x++)
{
cubePoints _= new Vector3(x * offset, 0);_
// need the cube for parenting spheres, keep it
var cube = Instantiate(cubePrefab, cubePoints*, Quaternion.identity) as GameObject;*
spherePoints = new Vector3[(sphereAmount + 1)];
cube.transform.SetParent(this.transform);
for (int j = 0, z = 0; z <= sphereAmount; j++, z++)
{
spherePoints[j] = new Vector3(0, 0, z + 1); // +1 because otherwise the first sphere is within the parent cube
// You might want to track the sphere instead of the vec3?
var sphere = Instantiate(spherePrefab, Vector3.zero, Quaternion.identity) as GameObject;
// parent
sphere.transform.SetParent(cube.transform);
// do positioning relative to parent
sphere.transform.localPosition = spherePoints[j]; ;
}
}
}
void Update()
{
Generate();
}
private void Generate()
{
GetComponent().mesh = mesh = new Mesh();
mesh.name = “Procedural Road”;
vertices = new Vector3[(cubeAmount + 1) * (sphereAmount + 1)];
Vector2[] uv = new Vector2[vertices.Length];
//I’m pretty sure this does nothing.
for (int i = 0; i < spherePoints.Length; i++)
vertices = spherePoints*;*
for (int i = 0, y = 0; y <= sphereAmount; y++)
{
for (int x = 0; x <= cubeAmount; x++, i++)
{
vertices = new Vector3(x * offset, 0, y + 1);
uv = new Vector2((float)x / cubeAmount, (float)y / sphereAmount);
}
}
int[] triangles = new int[cubeAmount * sphereAmount * 6];
for (int ti = 0, vi = 0, y = 0; y < sphereAmount; y++, vi++)
{
for (int x = 0; x < cubeAmount; x++, ti += 6, vi++)
{
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + cubeAmount + 1;
triangles[ti + 5] = vi + cubeAmount + 2;
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
if (vertices == null)
return;
Gizmos.color = Color.black;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices*, 0.1f);*
}
Gizmos.color = Color.green;
for (int i = 0; i < cubePoints.Length; i++)
{
Gizmos.DrawCube(cubePoints*, gizmoCubeSize);*
}
}
}
*
*
@Fattie