Hi, I’ve been batting my head against this for a bit so thought I’d ask here: I am instantiating objects on random points on a mesh, and want them to be aligned to the normals of those points, I’ve been through several forum threads and think I have done what’s needed re converting normals to rotations via Quaternion.Euler and so on but it still isn’t working. What am I missing?
public class InstOnMesh : MonoBehaviour {
public GameObject leg;
private GameObject[] legs;
private Vector3[] vertices;
private Vector3[] normals;
private Quaternion[] rotations;
private int thisLeg = 0;
private System.Random random;
void Start ()
{
Mesh mesh = gameObject.GetComponent<MeshFilter> ().mesh;
normals = mesh.normals;
vertices = mesh.vertices;
random = new System.Random ();
legs = new GameObject[10];
rotations = new Quaternion[vertices.Length];
for (int i = 0; i < vertices.Length; i++) {
rotations [i] = Quaternion.Euler (normals [i]);
if (random.NextDouble () < 0.2f && thisLeg < 10) {
Vector3 spawnPoint = transform.TransformPoint (vertices [i]);
legs [thisLeg] = Instantiate (leg, spawnPoint, rotations [i]);
legs [thisLeg].transform.SetParent (gameObject.transform, false);
thisLeg++;
}
}
}
}