Instantiating a Custom Prefab object based on Transform positions

Okay, the title is a little misleading so allow me to explain.

I have 3 empty GameObjects sitting in space. The position of these 3 GameObjects can change, as well as their distance from eachother.

I want to create a flat triangular plane between the three points that can be walked on. Therefore, creating the triangle model before hand would be a waste seeming as its three-corners can shift. I’m thinking this can be partly solved using some sort of transparent shader but I’m a noob at those.

Ultimately, I’d like to generate shapes based on these empty GameObject positions. 4 corners (quadrilateral) would be quite difficult seeming as sometimes the surface would need to curve around.

If anyone knows where I should start looking please let me know! Thanks :slight_smile:

You can use the Mesh class to create the triangle dynamically, where the 3 vertices in the mesh would use the positions of the GameObjects.

–Eric

Okay, so far I have what’s below.

It finds the three points and then creates a triangle. The problem is, it only accepts either clockwise or counter-clockwise. I need the triangle to be double-sided. Is there any way I can achieve this in one script rather than having to separate objects for each triangle? In other words, can I have a double-sided triangle as my mesh?

var material1 : Material;
var p1 : Transform;
var p2 : Transform;
var p3 : Transform;
var done = 0;

function Update() 
{
	if(done == 0  GameObject.Find("p1") != null  GameObject.Find("p2") != null  GameObject.Find("p3") != null)
		{	  
		  p1 = GameObject.Find("p1").transform;
	      p2 = GameObject.Find("p2").transform;
	      p3 = GameObject.Find("p3").transform;
	      
	      var mesh : Mesh = GetComponent(MeshFilter).mesh;
	      mesh.Clear();
	      mesh.vertices = 	[p1.position,p2.position,p3.position];      					
	      mesh.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2 (1, 1)];
	      mesh.triangles = [0, 1, 2];
	      	      
	      transform.GetComponent(MeshCollider).mesh = mesh;
	      
	      renderer.material = material1;
	      
	      GameObject.Find("p1").name = "p";
	      GameObject.Find("p2").name = "p";
	      GameObject.Find("p3").name = "p";
	      done = done + 1;
		}

As far as I know you cannot have double sided triangles. I think, you would have to have 2 separate triangles “back-to-back” but you don’t need a separate object for it. Just add vertices, etc… to the mesh you already have.

At first I didn’t know what you meant but then I changed:

mesh.triangles = [0, 1, 2];

to:

mesh.triangles = [0, 1, 2, 2, 1, 0];

Works, double-sided triangle. Thanks.