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 ![]()
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);
}
}