Change vertex color under player

Hello guys,

I have quite simple script that allows me to change mesh vertices colors to black. But I need to have a script that would change vertex color under player as he goes.

I wanted to ask what would be best approach for this that is also not too performance heavy as the mesh that player is using as a ground can have thousands of vertices.

The outcome I am going for is having mesh with whatever vertex colors and as player moves he would change those vertices to black.

Thank you.

For what you describe, you could try the following:
Cast a ray downwards. The resulting raycasthit saves the triangleIndex of the hit mesh:

Use that index to get the vertices that make up this triangle. Change their color.

However, for visual-only changes, one normally uses a shader. You’d have to look into it a bit. This was just a quick search, but looks roughly like what you are attempting: Mesh Texture painting in Unity Using Shaders | by Shahriar Shahrabi | Medium

Thank you! The raycast method is great for what I need.

Although I have a problem with instancing. Is it possible to make mesh modifications only on one instance? Basically if I have a function that changes vertex color on that mesh it rewrites that mesh basically. So if use fe. 10x10m square plane modularly and build level from those, it changes the vertex color on all of them rather than only on the one that player is currently on.

Thank you.