You have helped me out so much @bgolus so thanks for that, I had been researching this damn mechanic for months and was constantly running into problems so I’ll probably even update the original thread I made on the topic of influence borders generally and link this thread to help people stumbling on this problem as well.
There is only one final problem left, I have gotten the borders now fully functional and just do a simple boolean check on when the player exits and enters their own influence border. Now I can live with this issue it’s more an aesthetics thing but as you can see there’s ugly overlap popping back in again now that we’re generating an actual ring rather than a sphere like with the old way.
I remember seeing in my old thread people making lots of mentions about masking and so on? I have no idea though how you would potentially blend the borders in using masks with this method, do you have any ideas?
This by the way is how I’ve handled the collision, you’ll note as well that I’ve changed the colour according to ownership so that’s all functional now. There’s a sphere collider I’ve put in which I’ve linked up to the radius float that the mesh generation uses. I should be able to change that now according to things like how much population I have and so on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGeneratedInfluenceBorder : MonoBehaviour
{
public int sides = 16;
public float radius = 5f;
public float top = 1f;
public float bottom = -0.5f;
public Terrain terrain;
Mesh mesh;
Vector3[] vertices;
Vector2[] uvs;
int[] tris;
private void Update ()
{
CreateMesh();
GetComponent<SphereCollider>().radius = radius;
}
private void CreateMesh()
{
terrain = GameObject.FindGameObjectWithTag("Terrain").GetComponent<Terrain>();
if (mesh == null)
{
var meshFilter = GetComponent<MeshFilter>();
if (meshFilter == null)
{
return;
}
mesh = new Mesh();
meshFilter.sharedMesh = mesh;
GetComponent<SphereCollider>().radius = radius;
}
vertices = new Vector3[sides * 2 + 2];
uvs = new Vector2[sides * 2 + 2];
tris = new int[sides * 2 * 3];
Vector3 center = transform.position;
for (int i = 0; i <= sides; i++)
{
float radAngle = 2f * Mathf.PI / sides * i;
float s = Mathf.Sin(radAngle);
float c = Mathf.Cos(radAngle);
Vector3 pos = new Vector3(c * radius, 0f, s * radius);
float terrainHeight = terrain.SampleHeight(pos + center);
vertices[i * 2 + 0] = new Vector3(pos.x, terrainHeight + bottom - center.y, pos.z);
vertices[i * 2 + 1] = new Vector3(pos.x, terrainHeight + top - center.y, pos.z);
float u = (float)i / (sides);
uvs[i * 2 + 0] = new Vector2(u, 0f);
uvs[i * 2 + 1] = new Vector2(u, 1f);
}
for (int i = 0; i < sides; i++)
{
tris[i * 6 + 0] = i * 2 + 0;
tris[i * 6 + 1] = i * 2 + 1;
tris[i * 6 + 2] = i * 2 + 2;
tris[i * 6 + 3] = i * 2 + 2;
tris[i * 6 + 4] = i * 2 + 1;
tris[i * 6 + 5] = i * 2 + 3;
}
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = tris;
mesh.RecalculateBounds();
}
}
GetComponentInChildren<Renderer>().material.SetColor("_Color", Color.red);
Note: For people checking this thread out, this line is just a pseudo code example, obviously be sure to change GetComponent to suit your needs with how you’ve organised your hierarchy.