moving verts, drawing triangles

hey I’m making this thing where you can use the L-R-arrow keys to move the sides on the X axis of a cube and U-D-arrow keys to move the top and bottom of the cube on Y.
thats all fine (I think) but then redrawing my triangles gives me headaches…

the thing is i don’t want to draw them manually because i want to be able to use the script on almost any shape of model (but thats for later) for now I’m looking if there is a way to set my triangles with a simple algorithm, but one that connects the polygons. this what i have is drawing triangles per side (again i think)
if i got this correct (for a cube) every side has 8 verts, so thinking about that i don’t want to connect verts 4-5 to 0-1 but rather the 2 of the opposite side… so that if i move verts 0,1,2,3 (lets say if the obj is place in the worlds center thats the side on the positive x ) the new tris get drawn from the side on negative x to positive x… if that is understandable :wink:

using UnityEngine;
using System.Collections;

public class ManipulateSide : MonoBehaviour {
	
	public Mesh mesh;
	public float translateSpeed=2.0f;
	private Vector3[] vertices;
		
	void Start ()
	{
		if(mesh == null)
			mesh=gameObject.GetComponent<MeshFilter>().mesh;
	}
	// Update is called once per frame
	void Update () 
	{
		vertices = mesh.vertices;
		if(Input.GetAxis("Horizontal")>0)
		{
			for(int i=4 ; i< mesh.vertexCount; i++) //fix i to select right side (vert nrs)
			{
				vertices[i] =new Vector3(vertices[i].x + Input.GetAxis("Horizontal")*Time.deltaTime * translateSpeed, vertices[i].y, vertices[i].z);
				mesh.vertices = vertices;
				for(int ii = i; ii<4; ii++)
				mesh.triangles = new int[] {ii,ii+1,ii+2};
			}
		}
		if(Input.GetAxis("Horizontal")<0)
		{
			for(int i=4 ; i< mesh.vertexCount; i++) //fix i to select right side (vert nrs)
			{
				vertices[i] =new Vector3(vertices[i].x + Input.GetAxis("Horizontal")*Time.deltaTime * translateSpeed, vertices[i].y, vertices[i].z);
				mesh.vertices = vertices;
				for(int ii = i; ii<4; ii++)
				mesh.triangles = new int[] {ii,ii+1,ii+2};
			}
		}
        mesh.RecalculateBounds();
		//print(mesh.vertexCount);
	}
}

bumpy… maybe i didn’t explain it all to well.
i think what i’m looking for is an algorithm to draw triangles using shared vertices, is that possible or it the order for this different for every mesh?

ok this does it, anyway of writing it better?

using UnityEngine;
using System.Collections;

public class ManipulateSide : MonoBehaviour {
	
	public Mesh mesh;
	public float translateSpeed=2.0f;
	private Vector3[] vertices;
		
	void Start ()
	{
		if(mesh == null)
			mesh=gameObject.GetComponent<MeshFilter>().sharedMesh;
	}
	// Update is called once per frame
	void Update () 
	{
		vertices = mesh.vertices;
		if(Input.GetAxis("Horizontal")>0)
		{
			for(int i=0 ; i< 8; i+=2)
			{
				vertices[i] =new Vector3(vertices[i].x + Input.GetAxis("Horizontal")*Time.deltaTime * translateSpeed, vertices[i].y, vertices[i].z);
				mesh.vertices = vertices;
			}
		}
		if(Input.GetAxis("Horizontal")<0)
		{
			for(int i=1 ; i< 8; i+=2)
			{
				vertices[i] =new Vector3(vertices[i].x + Input.GetAxis("Horizontal")*Time.deltaTime * translateSpeed, vertices[i].y, vertices[i].z);
				mesh.vertices = vertices;				
			}
		}
		if(Input.GetAxis("Vertical")>0)
		{
			for(int i=3 ; i< 8; i+=4)
			{
				vertices[i] = new Vector3(vertices[i].x, vertices[i].y + Input.GetAxis("Vertical")*Time.deltaTime * translateSpeed, vertices[i].z);
				mesh.vertices = vertices;
			}
			for(int i=2 ; i< 8; i+=4)
			{
				vertices[i] = new Vector3(vertices[i].x, vertices[i].y + Input.GetAxis("Vertical")*Time.deltaTime * translateSpeed, vertices[i].z);
				mesh.vertices = vertices;
			}
		}
		if(Input.GetAxis("Vertical")<0)
		{
			for(int i=0 ; i< 2; i++)
			{
				vertices[i] =new Vector3(vertices[i].x, vertices[i].y + Input.GetAxis("Vertical")*Time.deltaTime * translateSpeed, vertices[i].z);
				mesh.vertices = vertices;				
			}
			for(int i=4 ; i< 6; i++)
			{
				vertices[i] =new Vector3(vertices[i].x, vertices[i].y + Input.GetAxis("Vertical")*Time.deltaTime * translateSpeed, vertices[i].z);
				mesh.vertices = vertices;				
			}
		}
		mesh.triangles = new int[] {0,1,3,
									2,0,3,
									0,2,6,
									0,6,4,
									5,4,6,
									5,6,7,
									1,5,7,
									7,3,1,
									5,1,0,
									0,4,5,
									7,6,3,
									6,2,3};
        mesh.RecalculateBounds();
	}
}