- Home /
How do I instantiate objects above 'sealevel'?
I have two procedurally generated meshes. One is my 'surface' layer, and the other is my 'sea' layer. Objects are instantiated on my surface layer, but even when the surface layer is below my sea layer. This is not intended.
So basically, I have 'land' objects spawning underwater. I do not want this.
Here is where the problem is:
if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y + 5f, randomZ), -Vector3.up, out hit)) {
if(r.bounds.max.y > -5) {
Instantiate(objectToPlace, hit.point, Quaternion.identity);
currentObjects += 1;
}
}
The
if(r.bounds.max.y > -5) {
is my attempt at fixing the problem, but it doesn't work. :(
Answer by zach-r-d · Jul 04, 2015 at 06:29 PM
Try putting a MeshCollider on the sea mesh, then checking if hit.collider is the land mesh or not. If it's not the land mesh, the raycast can be ignored.
Your answer
Follow this Question
Related Questions
How to Instantiate a prefab on the surface of a curved structure or mesh 1 Answer
Detecting a mouse click on a custom 2D mesh? 1 Answer
How can I instantiate an explosion using Raycasting? 0 Answers
Spawn script on mesh crashes Unity 0 Answers
How can I continue to instantiate an object after deleting 0 Answers