Hey guys need to create a Regular Octagon using Line renderer so what could be the perfect X,Y position numeric values for doing so… I was able to create a hexagon by watching brackeys video…but octagon is much more complex. Any help would be appreciated?
What have you tried so far?
I don’t believe it’s way more complex…
With the following code, you can have any regular shape you want:
public LineRenderer lineRenderer;
[Range(3, 20)]
public int positionsCount;
[Range(1, 10)]
public float radius;
[Range(0, 360)]
public float rotation;
public Vector3 normal = Vector3.forward;
[ContextMenu("OnValidate")]
private void OnValidate()
{
lineRenderer.positionCount = positionsCount;
Vector3[] positions = GetPoints( lineRenderer.positionCount, radius, rotation * Mathf.PI / 180f, normal ).ToArray();
lineRenderer.SetPositions( positions );
}
public List<Vector3> GetPoints( int sides, float radius, float rotation = 0 )
{
return GetPoints( sides, radius, rotation, Vector3.forward );
}
public List<Vector3> GetPoints( int sides, float radius, float rotation, Vector3 normal )
{
List<Vector3> points = new List<Vector3>( sides ) ;
float angle = 0 ;
normal.Normalize();
Vector3 right = Vector3.Cross( normal, Vector3.up ).normalized;
Vector3 up = Vector3.Cross( normal, right ).normalized;
if ( sides <= 1 )
{
points.Add( new Vector3( 0, 0 ) );
}
else
{
for ( int sideIndex = 0 ; sideIndex < sides ; sideIndex++ )
{
Vector3 point = up * Mathf.Cos( angle + rotation ) * radius + right * Mathf.Sin( angle + rotation ) * radius;
points.Add( point );
angle += Mathf.PI * 2f / sides;
}
}
return points;
}
private void OnDrawGizmos()
{
Gizmos.DrawLine( lineRenderer.transform.position, lineRenderer.transform.position + normal.normalized );
}
I have created a octagon by editing the points and using mouse and some maths…but without code[125948-octa.jpg|125948]