Hi,
I tried to draw a pentagon in unity inside a Panel that is a fraction of my top Canvas. I tried both with line render and Meshrender and I fail miserably with strange results. Is anyone kind enough to show me an example to do this? Thanks in advance
Option 1 (calculate the corners of a regular convex polygon by rotating a vector around the center of the polygon)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RegularConvexPolygon : MonoBehaviour
{
LineRenderer lineRenderer;
public int corners = 5;
void Start()
{
Vector3[] points = new Vector3[corners + 1];
Vector3 direction = new Vector3(0, 1, 0);
Quaternion angle = Quaternion.Euler(0, 0, 360f / (float)corners);
for (int i = 0; i < points.Length; i++)
{
points *= direction;*
direction = angle * direction;
// use this instead of the line above to create a regular star polygon
// direction = angle * angle * direction;
}
lineRenderer = gameObject.AddComponent();
lineRenderer.useWorldSpace = false;
lineRenderer.startWidth = 0.01f;
lineRenderer.endWidth = 0.01f;
lineRenderer.positionCount = points.Length;
lineRenderer.SetPositions(points);
}
}
----------
Option 2 (calculation of a corner is based on the previous corner)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pentagon : MonoBehaviour
{
LineRenderer lineRenderer;
void Start()
{
Vector3[] points = new Vector3[6];
Vector3 direction = new Vector3(1, 0, 0);
Quaternion angle = Quaternion.Euler(0, 0, 360f / 5f);
points[0] = Vector3.zero;
for (int i = 1; i < 6; i++)
{
points = points[i - 1] + direction;
direction = angle * direction;
}
lineRenderer = gameObject.AddComponent();
lineRenderer.useWorldSpace = false;
lineRenderer.startWidth = 0.01f;
lineRenderer.endWidth = 0.01f;
lineRenderer.positionCount = 6;
lineRenderer.SetPositions(points);
}
}