I need help making my array cast in the right spot, the offset should always be the size of the radius instead it is like a crazy small number and I cannot figure out how to get it to always be the size of the radius. the radius can change and I think the distance between the scene view and where it hits the cell will constantly change and there are too many variables and I do not fully understand how the rays work from the scene view editor camera to the mouse position so I cannot really do this by myself.
line 55 is where I need help for now multiplying the radius by itself seemed to make it work enough for me to know what to do, so that was just a temporary solution.
public static void TerrainHandler()
{
//The part that handles the editing of terrain.
if (editVertices)
{
//Handles the ray casting from the scenes camera to the mouses position.
Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(worldRay, out hitInfo))
{
if (hitInfo.transform.tag == TerrainSystem.cellTag)
{
//Use the point at which the ray hit the cell to determine where the mouse is at.
Vector3 mousePosition = hitInfo.point;
Debug.Log("The mouses position is: " + mousePosition);
Debug.Log("Main ray information: " + worldRay);
//Used to construct the bounds inside of which vertices will be edited.
float radius = (float.Parse(brushRadius));
GameObject cell = hitInfo.transform.gameObject;
Mesh mesh = cell.gameObject.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
if (flattenTerrain)
newHeight = float.Parse(flattenHeight) / (1 / gridLock);
for (int i = 0; i < vertices.Length; i++)
{
Vector3 worldPoint = cell.transform.TransformPoint(vertices[i]);
if (Vector3.Distance(mousePosition, worldPoint) <= radius)
{
Vector3 newPosition = new Vector3();
if (raiseTerrain)
newPosition = new Vector3(worldPoint.x, worldPoint.y + gridLock, worldPoint.z);
if (lowerTerrain)
newPosition = new Vector3(worldPoint.x, worldPoint.y - gridLock, worldPoint.z);
if (flattenTerrain)
newPosition = new Vector3(worldPoint.x, newHeight, worldPoint.z);
vertices[i] = cell.transform.InverseTransformPoint(newPosition);
}
}
Ray[] extraRay = new Ray[4];
RaycastHit extraHitInfo;
Vector2 mainRayPosition = Event.current.mousePosition;
float offSet = radius * radius;
extraRay[0] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x + offSet, mainRayPosition.y));
extraRay[1] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x - offSet, mainRayPosition.y));
extraRay[2] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x, mainRayPosition.y + offSet));
extraRay[3] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x, mainRayPosition.y - offSet));
for (int ray = 0; ray < extraRay.Length; ray++)
{
if (Physics.Raycast(extraRay[ray], out extraHitInfo))
{
int rayNumber = ray + 1;
Debug.Log("Ray " + rayNumber + " information: " + extraRay[ray]);
if (extraHitInfo.transform.gameObject != hitInfo.transform.gameObject)
{
GameObject extraCell = extraHitInfo.transform.gameObject;
Mesh extraMesh = extraCell.gameObject.GetComponent<MeshFilter>().mesh;
Vector3[] extraVertices = extraMesh.vertices;
for (int i = 0; i < extraVertices.Length; i++)
{
Vector3 worldPoint = extraCell.transform.TransformPoint(extraVertices[i]);
if (Vector3.Distance(mousePosition, worldPoint) <= radius)
{
Vector3 newPosition = new Vector3();
if (raiseTerrain)
newPosition = new Vector3(worldPoint.x, worldPoint.y + gridLock, worldPoint.z);
if (lowerTerrain)
newPosition = new Vector3(worldPoint.x, worldPoint.y - gridLock, worldPoint.z);
if (flattenTerrain)
newPosition = new Vector3(worldPoint.x, newHeight, worldPoint.z);
extraVertices[i] = extraCell.transform.InverseTransformPoint(newPosition);
}
}
extraMesh.vertices = extraVertices;
extraMesh.RecalculateBounds();
extraMesh.RecalculateNormals();
extraCell.GetComponent<MeshFilter>().mesh = extraMesh;
}
}
}
if (!cellsToSave.Contains(hitInfo.transform.gameObject))
cellsToSave.Add(hitInfo.transform.gameObject);
mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
cell.GetComponent<MeshFilter>().mesh = mesh;
}
}
}
foreach (GameObject cell in cellsToSave)
{
cell.GetComponent<MeshCollider>().sharedMesh = null;
cell.GetComponent<MeshCollider>().sharedMesh = cell.GetComponent<MeshFilter>().sharedMesh;
}
}