I am trying to edit 2 cells at once on my terrain, the code should fire 5 rays pretty much simultaneously and detect cells, if the main ray is hitting a cell this should fire and should fire 4 extra rays that determine if the main ray is near the edge of a cell and should pick up the cell it is near so that it is within the brush radius and can edit the cell without tearing a hole in the terrain, or at least keep holes to a minimum.
however it is not firing the extra 4 rays until the main ray changes to the next cell.
the problems lie on line 11 - 17 and 56 - 95 but I posted the whole function in case anyone wants to point out anything I can do to make this run faster or more efficient.
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;
Ray[] extraRays = new Ray[4];
RaycastHit extraHitInfo;
extraRays[0] = HandleUtility.GUIPointToWorldRay(new Vector2(Event.current.mousePosition.x + (float.Parse(brushRadius)), Event.current.mousePosition.y));
extraRays[1] = HandleUtility.GUIPointToWorldRay(new Vector2(Event.current.mousePosition.x - (float.Parse(brushRadius)), Event.current.mousePosition.y));
extraRays[2] = HandleUtility.GUIPointToWorldRay(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y + (float.Parse(brushRadius))));
extraRays[3] = HandleUtility.GUIPointToWorldRay(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y - (float.Parse(brushRadius))));
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;
//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);
}
}
foreach (Ray extraRay in extraRays)
{
if (Physics.Raycast(extraRay, out extraHitInfo))
{
if (extraHitInfo.transform.gameObject != hitInfo.transform.gameObject)
{
//Debug.Log("you should be at the side now.");
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);
Debug.Log("should have edited the vertices now");
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;
}
}
I’ve occasionally run into this sort of problem, and I almost always discover the raycast isn’t really hitting what I thought it hit. I’ve found the fastest way to debug the problem is to Debug.Log the instance ID of the RaycastHit’s GameObject, then use the debug inspector to see what it actually hit.
Your “extra rays” foreach loop is inside your main “if Raycast” block. You’re saying, if the main raycast hits, then loop through and check these other four rays, but if it doesn’t, then don’t check any of them.
I think you’d be best served by eliminating the outer “if Raycast” block entirely, and only keep the foreach block. And you can add the central ray to your extraRays array, but cast all of them at once.
Pretty sure that was intentional. It sounds like he doesn’t need to do the four “in range” checks if he doesn’t hit one of the cells. I was suggesting that maybe it hits something else, so the other four do run but since they’re offsets from something unexpected, the subsequent results aren’t causing the expected behavior.
I do need to hit it when I am not quite mousing over it, but the other 4 rays do not need to function if the main ray is not hitting a cell itself, this is meant for when I edit the edge of one of my cells it should still edit the edge of the other cell as well not just the one the ray is hitting. just got home and will debug and let you guys know what I am actually hitting with the other 4 rays
Edit: The ray is hitting where the main ray is, how can I make them fire where they are supposed to?
I have edited it a bit but not much, the way the debugs act it is firing when teh main ray first switches to a new cell, therefore I am led to believe that rays in teh foreach loop are firing at the mouse positiona nd not being offset properly.
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;
//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[] extraRays = new Ray[4];
RaycastHit extraHitInfo;
Vector2 mainRayPosition = Event.current.mousePosition;
float offSet = float.Parse(brushRadius);
extraRays[0] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x + offSet, mainRayPosition.y));
extraRays[1] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x - offSet, mainRayPosition.y));
extraRays[2] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x, mainRayPosition.y + offSet));
extraRays[3] = HandleUtility.GUIPointToWorldRay(new Vector2(mainRayPosition.x, mainRayPosition.y - offSet));
foreach (Ray extraRay in extraRays)
{
if (Physics.Raycast(extraRay, out extraHitInfo))
{
if (extraHitInfo.transform.gameObject != hitInfo.transform.gameObject)
{
Debug.Log(extraHitInfo.transform.gameObject.name);
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);
//Debug.Log("should have edited the vertices now");
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;
}
}
Not sure why this works but debugging I notices the origin of the rays was changing not the direction however it was changing a fraction of the brushes size.
cant tell you why but that is what I seen, so I thought I needed to devise a formula to make the offset = what it is supposed to, instead of actually creating a formula though I just multiplied it by itself to make it bigger. the results work great but I would still like a better formula to make teh offset = exactly what it is supposed to, not to mention I have only tested this with one or 2 smaller radius’s 5 and 6, so no telling what happens when I go very large.
if anyone could explain to me why this works teh 2way it does that would be nice to because I have no clue.
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;
}
}