How do i Gizmos.drawLines in an order??
Hello i am trying to do this in Pic1, i tried doing it but get this(Pic2).
-Pic1
-Pic2
Here is the code so you know what i have done.
using UnityEngine;
using System.Collections;
public class Lines: MonoBehaviour {
public int xSize, zSize;
Vector3[] points;
void Start()
{
CreateLines();
}
private void CreateLines()
{
points = new Vector3[xSize * zSize];
for (int i = 0, z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++, i++)
{
points[i] = new Vector3(x, 0, z);
}
}
}
void OnDrawGizmos()
{
if (points == null)
{
return;
}
for (int i = 0; i < points.Length; i++)
{
Gizmos.DrawSphere(points[i], 0.1f);
}
for (int i = 0; i < points.Length-1; i++)
{
Gizmos.DrawLine(points[i], points[(i + 1)]);
}
}
}
Answer by ScaniX · Sep 14, 2016 at 01:34 PM
If I didn't mix up xSize and zSize, this should do it:
for (int i = 0; i < points.Length-xSize; i++) {
Gizmos.DrawLine(points[i], points[i + xSize]);
}
Answer by Good_Venson · Sep 14, 2016 at 01:50 PM
What I would do is create a function to draw connected points.
void DrawPoints (Vector3 points) {
for (int p = 0; p < points.Length; p++) {
Gizmos.DrawSphere(points[p], etc...);
if (p < points.Length-1) Gizmos.DrawLine(points[p], points[p+1]);
}
}
And then call it in OnDrawGizmos.
float spacing = 1f;
int width = 3, height = 5;
private Vector3[] points;
void OnDrawGizmos () {
for (int x = 0; x < width; x++) {
points = new Vector3[height];
for (int y = 0; y < height; y++) {
points[y] = new Vector3(x*spacing, y*spacing, 0f);
}
DrawPoints(points);
}
}
Yes, the points are being created every OnDrawGizmos, but there aren't that many points, and if it was a problem, you could just have an array of arrays of points that you create in Start or wherever. The main thing is the orientation. Just make sure that your for loops are inside each other correctly.
Your answer
Follow this Question
Related Questions
How do i use lines to calculate triangles on mesh?? 0 Answers
OnCollisionEnter not running. 1 Answer
How do i create Tangents on a spline?? 0 Answers
[HELP] To show wave number on a zombie survival game. 0 Answers
How to make the character walk-jump and walk (and resize the collider when crouched) ? 0 Answers