Hello, is there any possible way to set the height between specific vertices?
This is basic code for creating a grid below, but how could i set the heights of just the edge cells, and leave the middle flat? or vice versa. Is there a way to specify which vertices get adjusted??
im pretty new at C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class GridPlacement : MonoBehaviour
{
public int xSize, zSize;
private Vector3[] vertices;
private int[] triangles;
Mesh mesh;
public void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
Generate();
UpdateMesh();
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
private void Generate()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++, i++)
{
float y = Mathf.PerlinNoise(x * 0.3f, z * 0.3f) * 2f;
vertices[i] = new Vector3(x, y, z);
}
}
int vert = 0;
int tris = 0;
triangles = new int[xSize * zSize * 6];
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
}
private void OnDrawGizmos()
{
if (vertices == null)
{
return;
}
Gizmos.color = Color.green;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], 0.1f);
}
}
}