- Home /
Vertices Array only given in local coords?
Hi all. I'm trying to make an object that stores it's starting point, moves forward for a given time, then returns and repeats ad infinatum. I've done this with a coUpdate to reset the position, no problem.
However, next, I want the gameobject to make an array of it's vertices, choose one at random, make it the start point for a line renderer, move forward a bit, do the same again for a second random point for the second point in the line renderer, etc, so the effect is a random, wiggly line that zigzags out in one direction, then disappears, and repeats.
I'm almost there under my own steam, but it seems the Vector3 array created for the vertices of the object are in local space. I get the object moving and snapping back to the start, but the linerender shoots along the path of the object instead of staying in world space like I want. Can anyone tell me what I'm doing wrong please? My code:
var startPos : Vector3;
var speed = 1.0;
function Start () {
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = mat;
lineRenderer.useWorldSpace= false;
lineRenderer.SetWidth(0.2,0.2);
coUpdate();
startPos = transform.localPosition;
}
function FixedUpdate()
{
transform.Translate(Vector3.forward, Space.Self);
}
function coUpdate()
{
while(true)
{
transform.localPosition = startPos;
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
for (var i = 0; i < vertices.Length; i++)
var randNo1 = Random.Range(1,20);
var point1 = vertices[randNo1];
yield WaitForSeconds (stepTime/3);
var randNo2 = Random.Range(1,20);
var point2 = vertices[randNo2];
// lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(0, point1);
lineRenderer.SetPosition(1, point2);
yield WaitForSeconds (stepTime/3);
var randNo3 = Random.Range(1,20);
var point3 = vertices[randNo3];
lineRenderer.SetVertexCount(3);
lineRenderer.SetPosition(2, point3);
yield new WaitForSeconds(0.2);
}
}
(excuse the commented out part, I've tried a few different ways!)
I don't expect or want anyone to write the code for me, but I just need a nudge in the right direction...
Cheers
Ian
Answer by robertbu · Aug 15, 2013 at 08:07 PM
With just a quick read of the code, I suggest that you change to using world coordinates:
var point1 = transform.TransformPoint(vertices[randNo1]);
You will do the same for 'point2'.
Then for the line renderer you will use world space:
lineRenderer.useWorldSpace= true;
Almost - I get a new type of weirdness now! I changed just the first point to your code, expecting a shooting line with a fixed start. It does this, but the start point is very far away, at a random (but constant) part of the map, definitely not 0,0,0 either. Thanks for the input. I'm currently experimenting with instantiating empties at the points...
Ignore me, I was being daft. That works perfectly, thanks!
Actually in running some tests, I was wrong about the local scale.
While you have it solved, I'll post a bit of test script for any future person looking at this issue. It draws a line between two random points in the mesh:
private var vertices : Vector3[];
private var lineRenderer : LineRenderer;
function Start() {
vertices = GetComponent($$anonymous$$eshFilter).mesh.vertices;
lineRenderer = GetComponent(LineRenderer);
DrawLine();
}
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
DrawLine();
}
}
function DrawLine() {
//var point1 = Vector3.Scale(vertices[Random.Range(0,vertices.Length)], transform.localScale);
var point1 = transform.TransformPoint(vertices[Random.Range(0,vertices.Length)]);
do {
//var point2 = Vector3.Scale(vertices[Random.Range(0,vertices.Length)], transform.localScale);
var point2 = transform.TransformPoint(vertices[Random.Range(0,vertices.Length)]);
} while (point1 == point2);
lineRenderer.SetPosition(0,point1);
lineRenderer.SetPosition(1,point2);
}