Hi, I have a cube, let’s call it player, under that i have a procedurally generated plain, the palyer is going to move or rather float above and along that plain, I want to:
A) get the vertices around the cube that are in the radius of the player.
now i have achived that by looping through all vertices in that plain but that massivly cuts performance.
i want to be able to get the vertices that are in the radius with somthing like physics.overlapsphere
without having to loop through all the vertices, this obviusly doesn’t work since overlapsphere ruturns a collider array and i want vertices which are a verctor3.
B) raise those vertices on the y axis relative to the players position (the closer the vertex is to the player the higher it is), now i have also gotten this to work but its not as nice and smooth as i want it to be, the vertices just lerp the Y axis to 3 if they are in the radius, and if not the don’t move at all.
i want it to more dependent on more of where the player is rather than if its in the radius,
If you get what i mean.
Here is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
//player's refrence
public Transform player;
//minimum distance for the vertex from the player to be affected and raised up
public float minDistance;
//limit for the vertices on the Y axis
public float meshOffset;
//mesh generation stuff
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public int xSize = 20;
public int zSize = 20;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
void Update()
{
MoveVertices();
}
//Basically creating a plain at runtime to have a refrence to the vertices
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
vertices *= new Vector3(x, 0, z);*
i++;
}
}
triangles = new int[xSize * zSize * 6];
int vert = 0;
int tris = 0;
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++;
}
}
//updating the meshes vertices and triangles and recalculating lighting on the surface of it
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
//Using the players refrence to get his pos and looping through the vertices array to find which vertices are in the redius of the player
// This is the part that i am not certain of
void MoveVertices()
{
for (int i = 0; i < vertices.Length; i++)
{
float distance = Mathf.Sqrt((player.position - vertices*).sqrMagnitude);*
if (distance < minDistance)
{
vertices.y = Mathf.Lerp(vertices_.y, meshOffset, Time.deltaTime * distance);
UpdateMesh();_
}else
{
vertices.y = Mathf.Lerp(vertices_.y, 0, Time.deltaTime * distance);
UpdateMesh();
}
}
}_
private void OnDrawGizmos()
{
if (vertices == null)
return;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices*, 0.1f);*
Gizmos.color = Color.red;
}
}
}
for visualization, i am basically triying to mimic geometry wars meshs and how they react to objects positions and what’s happening on the battle field
*30 Minutes of GEOMETRY WARS 3 Gameplay! Classic Mode, Pacifism, Co-Op and More - YouTube
As always, any help is appreciated
Thanks in advance!_