- Home /
Flattening a mesh based on hit.point
How do you flatten a mesh based on the point of impact or hit.point?
that sounds like the hard way to do it. I believe you could use it with a shader that utilizes displacement map ins$$anonymous$$d? depending on your primary goal ofcause. Just remember that Unity isnt a 3D modeller tool. Its a Game/visualization engine.
Honestly, I'd do it with a skinned mesh animation. $$anonymous$$ake it in your 3d modelling application of choice, and then blend the animation in when the time comes.
Answer by Foxis · Nov 02, 2011 at 01:11 PM
If you just need to edit the graphical representation, it is quite easy and not that much of a performance impact. You have the hitpoint, the mesh and the transform, so you can access the vertices like so:
hittransf = hitinfo.collider.transform;
tri = hitinfo.triangleIndex;
verts = hitmesh.vertices;
tris = hitmesh.triangles;
thevertex = hittransf.TransformPoint(verts[tris[tri*3]]);
and to modify the vertex you do:
verts[tris[tri*3+thevertex]] += Vector3.up * ammount;
I just snipped this out of a project, so please take into account that some of the variables above are already set. For example, you need to grab the mesh (hitmesh aboive) and after the operation you need to set the vertices of the mesh to the edited array (verts).
This should get you started. Hope it helps! :)
I guess my question is more specific than accessing the vertices... how do you modify it so the mesh gets flattened in area or point of hit.
not sure exactly how you got these indices? verts[tris[tri*3+thevertex]]
Take a look again. ;)
The key index is tri, and it is retrieved from tri = hitinfo.triangleIndex. hitinfo is your hit information from the collider involved. tri is then used to get the vertices you want to modify. The last code line above was originally wrapped in a for loop where thevertex was incremented to affect the entire triangle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030529im_/https://answers.unity.com/themes/thub/images/avi.jpg)