- Home /
How do i instantiate a Vector3[] ??
hello i am having some trouble trying to instantiate 'Vector3[] newVerts' on 'Vector3[] origVerts' like whats in Pic1 what i am trying to do is spawning newVerts on each origVerts, but the results i am getting are in Pic2,
i don't want to use the Instantiate function because i don't want to apply a transform/gameobject to it, does anyone know how to do this? @damagefilter
(i'll put my code down below so you know what i have done)
-Pic1. ( what i would like to achieve )
-Pic2. ( The Results i get with my code below )
Here is the code so you know what i have done.
using UnityEngine;
using System.Collections;
public class Extruder : MonoBehaviour {
public float rotAngle;
public int stretch = 5;
private MeshFilter mf;
private Vector3[] origVerts;
private Vector3[] newVerts;
void Start()
{
mf = GetComponent<MeshFilter>();
origVerts = mf.mesh.vertices;
newVerts = new Vector3[origVerts.Length];
Generate();
}
void Generate()
{
for(int i = 0, x = 0; i < stretch; i++ ,x++)
{
newVerts[x] = new Vector3(0,0,i);
}
mf.mesh.vertices = newVerts;
}
void OnDrawGizmos()
{
Gizmos.color = Color.white;
for (int i = 0; i < newVerts.Length; i++)
{
Gizmos.DrawSphere(newVerts[i], 0.1f);
}
Gizmos.color = Color.green;
for (int i = 0; i < origVerts.Length; i++)
{
Gizmos.DrawSphere(origVerts[i], 0.2f);
}
}
}
Downvoted for abusing the notification system. It's abuse like this that caused me to have to disable notifications for user tagging. Way to ruin things for other people.
Abusing the notification system, i cant use user tagging, wtf, why?
You have no reason to tag users in a question like that. It's rude and disruptive to get notifications for something you're not involved with, not that I should even have to explain this. Nobody here is your personal answering service. The way it works is, you post a question, and anyone who has the knowledge and inclination posts an answer. You can use tagging once someone has become involved in the question by posting comments/answers.
Answer by saschandroid · Aug 23, 2016 at 08:44 AM
I guess the problem is that you are generating each new vertex at a different stretch position (z=i). You have to use 4 (or orgVertex.Length) times the same stretch value
void Generate()
{
for (int i = 0; i < stretch; i++)
{
for (int x = 0; x < orgVerts.Length; x++)
{
newVerts[x] = new Vector3(origVerts[x].x,0,i);
}
}
mf.mesh.vertices = newVerts;
}
Sorry, I didn't test it ... here's the update:
void Start()
{
...
newVerts = new Vector3[origVerts.Length * stretch];
...
}
void Generate()
{
for (int i = 0; i < stretch; i++)
{
for (int x = 0; x < origVerts.Length; x++)
{
newVerts[x + i * origVerts.Length] = new Vector3(origVerts[x].x, origVerts[x].y, i);
}
}
...
}
Answer by aditya · Aug 23, 2016 at 04:57 AM
Your Generate
method should be like this
void Generate()
{
for(int i = 0, x = 0; i < stretch; i++ ,x++)
{
newVerts[x] = new Vector3(origVerts[x].x,0,i);
}
mf.mesh.vertices = newVerts;
}
Sadly your method does not work as you can see in the picture below and i also get ERROR "Array index is out of range".
Then simply change the Y element too ...
newVerts[x] = new Vector3(origVerts[x].x,origVerts[x].y,i);
If this helped please accept the answer
I have already tried that, it doesn't work D: It gives a similar result.