Edit, K, so I edited out the problem ‘if statements’ so the variables could be assigned…
Hi, I’m trying to use Raycasts to set nodes at various points on terrain and/or ocean. I want to compare the distance of two different ray hits and take the longest one to set a node position.
I’m running into problems at the end where I try to compare the two and set the spawnPos vector.
I get Error: use of unassigned local variable
Vector3 dir = -(planetCenter - gridPositions[i]).normalized;
Vector3 spawnPos;
Ray ray1 = new Ray();
Ray ray2 = new Ray();
ray1.origin = planetCenter;
ray1.direction = dir;
ray2.origin = planetCenter;
ray2.direction = dir;
RaycastHit hit;
RaycastHit hit2;
Vector3 OceanHit;
Vector3 TerrainHit;
float OceanRaydist;
float TerrainRaydist;
Physics.Raycast(ray1, out hit, MaxMountainHeight, water);
OceanHit = hit.transform.position;
OceanRay = hit.distance;
Physics.Raycast(ray2, out hit2, MaxMountainHeight, terrain);
TerrainHit = hit2.transform.position;
TerrainRay = hit2.distance;
if (TerrainRaydist >= OceanRaydist)
{
spawnPos = TerrainHit;
}
else
{
spawnPos = OceanHit;
}
Never M
Hey,
Of course it doesn’t work, if the Raycast returns false, your variables TerrainHit etc … will never be initialized.
But you are doing it wrong, you should put the Terrain and the Ocean on a same layer and use RaycastAll that returns all objects hit by your ray. After that you can sort by distance all the result and pick the one you want. And it will work if the ray hit just one, none or both of them.
RaycastHit[] hitsInfo = Physics.RaycastAll(ray, float.PositiveInfinity, layerMask); // Returns all object hit by your ray
hitsInfo.OrderBy((hit) => hit.distance); // You can use Linq to sort the array, or do it by yourself in a for if you need performances. Doing it with the for is the best solution but I am lazy to write it now =)
Thanks I realized the If statements were the problems and I wasn’t sure how to get/set the furthest of the RayCastAll hits as spawnPos. Again… noob alert. Since there should only be two hits by any one ray I figured using two separate rays would not slow things down too much…
but… is there a simple way to set spawnPos to the largest of the hit distances?
But now I’m getting a different problem when the script is actually ran. This error corresponds to line 20 of the code above
NullReferenceException: Object reference not set to an instance of an object
SphericalGrid.CreateNodes () (at Assets/_SphericalPathfinding/Code/Pathfinding/SphericalGrid.cs:193)
SphericalGridEditor.BakeNodeProcess () (at Assets/_SphericalPathfinding/Code/Editor/SphericalGridEditor.cs:145)
UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorApplicationBindings.gen.cs:264)
Oops, I think it’s not using the array right. Here’s the original way to get spawnPos that I’m trying to modify… Instead of going out a fixed radius I want each spawnPos to be set on the position of the actual spherical terrain.
Vector3 spawnPos = -((planetCenter - gridPositions[i]).normalized) * planetRadius;
any help on how I can modify my code to work with the array*?* I thought line one of the original post covered it…