- Home /
Question by
mrapplegekk · Jun 07, 2020 at 06:49 PM ·
c#meshmesh manipulationmesh-deformation
Moving an hole in a plane
Hi i saw in a video on Youtube a script to modify the position of an Hole in a plane. I tried this script and it worked well, and i thought that it could be perfect for my project. When i tried to apply the script in more object it didn't work, so i tried many different ways to solve this problem. After many researches and tests i found out that if the script, doesn't have attached an object in position(0,0,0) dosen't work. I just want to know if there is a solution to my problem, and if someone could explain to me why is that.
This is the script:
[Header ("Hole Mesh")]
[SerializeField]
private MeshFilter _holeMeshFilter;
[SerializeField]
private MeshCollider _holeMeshCollider;
[Header("Hole Radius")]
[SerializeField]
private float _holeRadius;
[SerializeField]
private Transform _holeCenter;
private Mesh _mesh;
[SerializeField] private List _holeVertices;
[SerializeField] private List _offset;
[SerializeField] private int _holeVerticesCount;
private void Start()
{
_holeVertices = new List<int>();
_offset = new List<Vector3>();
_mesh = _holeMeshFilter.mesh;
FindHoleVertices();
}
private void Update()
{
UpdateHoleVertices();
}
private void FindHoleVertices()
{
for (int i = 0; i < _mesh.vertices.Length ; i++)
{
float distance = Vector3.Distance(_holeCenter.position, _mesh.vertices[i]);
if(distance < _holeRadius)
{
_holeVertices.Add(i);
_offset.Add(_mesh.vertices[i] - _holeCenter.position);
}
}
_holeVerticesCount = _holeVertices.Count;
}
private void UpdateHoleVertices()
{
Vector3[] Vertices = _mesh.vertices;
for (int i = 0; i < _holeVerticesCount; i++)
{
Vertices[_holeVertices[i]] = _holeCenter.position + _offset[i];
}
_mesh.vertices = Vertices;
_holeMeshFilter.mesh = _mesh;
_holeMeshCollider.sharedMesh = _mesh;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(_holeCenter.position, _holeRadius);
}
Thanks in advance.
Comment