Hello community,
I’m a student working on a flight simulator project with Unity. I used Cesium because I wanted to create a mountain range. My goal is to have the plane follow a trajectory at a certain altitude, while avoiding mountain terrain (the Himalayas in this case). I’m using a function that contains a raycast to obtain the height between the plane and the ground and thus determine if the plane can lower its altitude. The problem is that the raycast doesn’t hit anything. However, the collisions are active since I manage to crash the plane into the mountains.
Do you have any ideas or perhaps a clue? I don’t understand how collisions can be active but not detect anything at the same time.
The same is true when I use procedural terrain.
It’s hard to say what might be wrong without seeing your code. Since you say it’s not working with procedural terrain either, perhaps the direction of your ray cast is incorrect?
I tried again several times. And finally, I managed to make the plane follow the terrain, avoiding the relief. However, my raycast continues to not touch anything. So I’m not sure how to explain this. Here is the snippet of code where I use the raycast to get the height of the terrain.
I think I’ll still have trouble keeping the plane’s altitude low enough, but I haven’t tried it yet.
float GetTerrainHeight(Vector3 position)
{
Vector3 rayOrigin = new Vector3(position.x, 10000f, position.z); // On part de très haut
if (Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit hit, Mathf.Infinity))
{
Debug.DrawLine(rayOrigin, hit.point, Color.red, 5f);
Debug.Log("Raycast touche le terrain à : " + hit.point.y);
return hit.point.y;
}
else
{
Debug.LogWarning("Raycast n’a rien touché pour la position : " + position);
// Valeur de secours : on prend la hauteur actuelle de l’avion moins heightAbovePath
return airplane.position.y - heightAbovePath;
}
}
I noticed that you’re using Vector3.down as the direction to raycast. Maybe you’d have more luck if you compute the direction from the center of the Earth?
This is just pseudocode, but maybe something like this:
CesiumGeoreference georeference; // Get and save a reference to this in OnEnable()
double3 centerOfEarth = georeference.TransformEarthCenteredEarthFixedPositionToUnity;
double3 direction = normalize(centerOfEarth - rayOrigin);
if (Physics.RayCast(rayOrigin, direction, ...)) {}