Why does my generated mesh have an offset?
Hello, everyone! I've encountered a problem when my mesh has an offset. I wanted to create a mesh around an object _objAround
. But something went wrong.
First, I give the mesh coordinates via script _meshCoords
. Second, I apply the same position to my mesh object as the object-around's one transform.position = _objAround.position
. Third, I create new positions for the mesh this way: var meshNewPosition = transform.position + _meshCoords[i]
. But as a result, it didn't work out.
There are three objects in the unity inspector: "Cubes" containing cubes representing the coordinates of the mesh, "Object Around" - an object I create mesh around and "Mesh" - an object containing the mesh and having the script.
Although the mesh box collider took the object-around position, the mesh still didn't (you can see it in the image). And also, I noticed that while the mesh pivot was at the object-around position, the center point was not (you can see it in the image as well).
Please, help me. Thanks in advance!
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class MeshCreator : MonoBehaviour
{
[SerializeField] private Vector3[] _meshCoords;
[SerializeField] private Transform[] _cubes;
[SerializeField] private Transform _objAround;
private Mesh _mesh;
private void Start()
{
_mesh = new Mesh() { name = "Test mesh" };
GetComponent<MeshFilter>().sharedMesh = _mesh;
transform.position = _objAround.position;
}
private void Update()
{
GenerateMesh();
}
private void GenerateMesh()
{
_mesh.Clear();
// Vertex Initialisation
var vertices = new List<Vector3>();;
for (int i = 0; i < _meshCoords.Length; i++)
{
var meshNewPosition = transform.position + _meshCoords[i];
vertices.Add(meshNewPosition);
_cubes[i].position = meshNewPosition;
}
// Triagnles Initialisation
var triangles = new int[]
{
0, 1, 3,
1, 2, 3,
};
_mesh.SetVertices(vertices);
_mesh.SetTriangles(triangles, 0);
}
}
That's before any code working
Images: https://imgur.com/a/0MglSdV