- Home /
How do i create multiple vertices from an object?
Hello, this is rather a simple question but i cant really understand on how to do it, i am trying to achieve something like this in Pic1, but the results i am getting are in Pic2. Does anyone know how to do this?
NOTE: there is a post on about this at this site
-Pic1. -Pic2. Here is what my code so you know what i have done.
using UnityEngine;
using System.Collections;
public class Extruder : MonoBehaviour {
public Mesh prefab;
public int length;
private Mesh mesh;
private Vector3[] vertices;
void Start()
{
vertices = prefab.vertices;
Generate();
}
// Update is called once per frame
void Generate () {
vertices = new Vector3[(length)];
for (int i = 0, x = 0; x <= length; i++, x++)
{
vertices[i] = new Vector3(0, 0, x);
}
mesh.vertices = vertices;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.black;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], 0.1f);
}
}
}
Answer by Fattie · Aug 20, 2016 at 11:37 PM
um - your problem is X and Y are always the same, in fact zero.
vertices[i] = new Vector3(0, 0, x);
you are setting them to zero every time.
you want to weave FOUR points. something like this ...
you have this:
vertices[i] = new Vector3(0, 0, x);
you should have this:
vertices[i] = new Vector3(-1, -1, x);
vertices[i] = new Vector3(-1,1, x);
vertices[i] = new Vector3(1, 1, x);
vertices[i] = new Vector3(1, -1, x);
it is very common when you are weaving mesh that you have code like that: for each trip through the loop you may be making say 6, 8 or whatever points.
try to travel around anticlockwise if possible, just as a matter of style. note you will absolutely need to become expert at using gizmo balls during development if you are not using them already
(1) I would just point out that "generating mesh" is pretty difficult; you probably still have a ways to go and many other problems to solve
(2) this site answers.unity3d just works really poorly these days. for example I am never logged in, it seems to constantly log me out, and the site barely works and has many bugs. StackOverflow is incredibly lame and uncool, but there are nowadays huge numbers of Unity questions on there. I would probably encourage you to ask on there.
There are many superb essays by people both intelligent and witty on there, often sober too :) example pls go here http://stackoverflow.com/a/35891919/294884 and vote it up. see you there
Thanks but ins$$anonymous$$d of creating the points individually, how will i make it where i can just add a mesh and gather its vertices then multiply them across an axis?
I have registered to stackoverflow as TheRapidSloth i'll be checking out that site.
If you mean you want to take a shape and "extrude" it. Realistically you would just get an asset from the asset store to do that. It takes some six man-months of work to write extrusion software. Just look on the software for mega-shapes, ProSplines and so on. Cheers.
Thanks, but i would like to know how to code it, and not to go to the asset store and purchase an asset, would you know any tutorials or sites i could learn this from?