- Home /
Getting multiple heights at same (x,y) coordinate
So I've created a model landscape on blender that contains an underpass. The game that I'm making involves a TileMap (grid-based system). Each tile will contain information about its location (x,y coordinates, height, tile type etc...) The problem I've run into is that most threads talk about utilizing RayCasting in order to get height information, but my model is one mesh containing one collider and raycasting (as far as I know) cannot bypass a "single layer of material". My question is how can I acquire height information at this x,y point for both the underpass and the bridge above? Should I go back into blender and separate the model so each item can have separate colliders and use tag, or is there another method I should look into that might work efficiently? I want to target mobile platforms so I'd be great if there resource sparing methods out there!
EDIT:: Thanks to @Nischo for the answer, I've implemented the Physics.RayCastAll method into my program in the following manner:
void CreateTileMap(){
Debug.Log ("Create TileMap is called");
int numXYtiles = terrainX * terrainZ;
RaycastHit[] detects;
float tileHeight;
Vector3 detectedHeight;
Vector3 rayOrigin = new Vector3 (0f, 15f, 0f);
Vector3 rayDirection = new Vector3 (0, -1, 0);
for (int x = 1; x < (terrainX*2); x = x + moveUnit) {
rayOrigin.x = (float) x;
//Debug.Log (rayOrigin.x);
for (int z = 1; z < (terrainZ*2); z = z + moveUnit){
rayOrigin.z = (float) -z;
//Debug.Log (rayOrigin.z);
detects = Physics.RaycastAll(rayOrigin, rayDirection);
for (int i = 0; i < detects.Length; i ++) {
//Debug.Log (i);
RaycastHit detect = detects[i];
Debug.Log (detect.distance);
detectedHeight = new Vector3(rayOrigin.x, rayOrigin.y - detect.distance, rayOrigin.z);
newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
newSphere.transform.position = detectedHeight;
}
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = rayOrigin;
}
}
}
I've also implemented a Debug.Log(detects.Length) to check whether multiple hits were detected and unfortunately all the values were 1. So only the first hit was registered, for more clarity I've attached a visualization here:
So as you can see the underpass has no spheres and therefore is not being detected. Any way around this?
Answer by Nischo · Jul 09, 2015 at 01:06 PM
If i understand you correctly you want to get a point on your terrain, which might contain multiple "levels" of ground like a mountain and directly underneath a tunnel. Then yes, a simple Raycast might not be enough, but what you could do is raycast with Physics.RaycastAll sort the results by height and use the one thats most appropriate at that point.
Your answer
Follow this Question
Related Questions
3D Tilemap 1 Answer
Simple mesh height alteration. 1 Answer
Detecting a mouse click on a custom 2D mesh? 1 Answer
Spawn script on mesh crashes Unity 0 Answers