Hi,
Given any 3D model, I’m trying to get the position of all vertices that it has and instantiate a sphere primitive at runtime. I was able to get the initial position of the vertex point correct but when I try to rotate the model I can’t seem to update the position of the sphere markers. Please see my code below:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VertexPointHandler : MonoBehaviour {
private Mesh mesh;
private Vector3[] vertices;
public float speed = 5.0f;
private List<GameObject> markerList = new List<GameObject>();
public GameObject marker;
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
foreach (Vector3 v in vertices)
{
Instantiate(marker);
Vector3 worldPt = transform.TransformPoint(v);
marker.transform.position = worldPt;
markerList.Add(marker);
}
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
mesh.RecalculateBounds();
Vector3[] vertices_update = mesh.vertices;
for (int i=0; i< vertices_update.Length; i++)
{
Vector3 worldPt = transform.TransformPoint(vertices_update[i]);
markerList[i].transform.position = transform.position + worldPt;
//markerList[i].transform.position = transform.position + vertices[i];
}
}
}
Start:
After cube rotation:
Thanks everyone!