Modifying vertices values in a mesh

I’ve been trying to apply brushes as heightmap on a sphere but there’s some weird stuff going on that I can’t find an answer to.

What is happening is that when I apply the heightmap on a vertex, it seems like it’s not affecting the good vertex. I’m checking if from the center in the direction of the vertex there’s a collision with a brush and if so, I’ll add a vector3 in the same direction as the raycast used but as you can see in the image below, it affects the vertex on the left side instead of on the top of the sphere.

The code is straightforward but I can’t seem to understand what’s happening and why.


Also, if the brush is on x axis, it appears fine but otherwise, the z and y are invert.

The code I used is:

void Start () {
		Mesh mesh = GetComponent<MeshFilter>().mesh;
		Vector3[] vertices = mesh.vertices;
		Vector3[] sphereVertices = new Vector3[mesh.vertexCount];
		for(int i=0; i<vertices.Length; ++i){
			Vector3 vertex = vertices_.normalized * 3;_
  •  	RaycastHit info;*
  •  	if(Physics.Raycast(, vertex, out info)){*
  •  		Vector3 addHeight = vertex.normalized;*

addHeight *= GetGrayscale(info.collider.transform.renderer.material.GetTexture(“_MainTex”) as Texture2D,info.textureCoord);

  •  		vertex += addHeight;*
  •  	}*

_ sphereVertices = vertex;_
* }*

* mesh.vertices = sphereVertices;*
* mesh.RecalculateNormals();*
* }*

I don’t know if this is the issue without more study, but your vertices are in local space and your brush is in world space. You may need to do a Transform.TransformPoint(). If you sphere is not at the origin, you also have to take that into account:

RaycastHit info;
Vector3 v3 = transform.TransformPoint(vertex) - transform.position;
if(Physics.Raycast(, v3, out info)){