Cos/Sin help

Hello i am trying to make a circular mesh, the problem i encountered seems to be that the vertices do not align properly to form a perfect circle and so i get this. ( i created box primitives at the vertice vec3 locations as i have yet to define the UV’s and Triangles to form the mesh ).

PICTURE:

here is the code i am currently using.

using UnityEngine;
using System.Collections;

public class CreateAsteroid : MonoBehaviour
{

   int AsteroidMeshVerticeNumber = 8 ;
   int AsteroidMeshRadius = 32;
   public Vector3[] AsteroidMeshVertices;

   void Start ()
   {
     gameObject.AddComponent("MeshFilter");
     gameObject.AddComponent("MeshRenderer");

     Mesh AsteroidMesh = GetComponent<MeshFilter>().mesh;
     AsteroidMesh.Clear();
     
     AsteroidMeshVertices = new Vector3[AsteroidMeshVerticeNumber];

     for (int i = 0; i < AsteroidMeshVerticeNumber; i++)
     {
       float _angle = (360 / AsteroidMeshVerticeNumber) * i ;
       Debug.Log(_angle);
       
       float _x = Mathf.Cos( _angle ) * AsteroidMeshRadius ;
       float _z = Mathf.Sin( _angle ) * AsteroidMeshRadius ;
       //Debug.Log(_x);
       //Debug.Log(_z);
       
       AsteroidMeshVertices[i] = new Vector3( _x , 0 , _z );
       
       GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
       cube.transform.position = new Vector3( _x , 0 , _z );
     }
   }
}

Thank you for your help!

Dmajster_

The Sin and Cos methods expect angles in Radians, you are using degrees.

1 Like

The sin/cos functions take an input in radians, it looks like you’re trying to pass it in as degrees.

You can do ‘_angle * Mathf2.Deg2Rad’ to convert - that should work.

To make it more accurate I would change this:

float _angle = (360 / AsteroidMeshVerticeNumber) * i ;

to

float _angle = (360.0f / (float)AsteroidMeshVerticeNumber) * i;

If the AsteroidMeshVerticeNumber was something like 13 then the integer arithmetic might give you a slightly incorrect angle.

T

1 Like

Thank you both! The problem is resolved now!