I have an object which I need to reposition randomly.
I need to make sure I don’t position it inside parts of the landscape, which is also generated randomly.
OnTriggerNull would be nice, but there’s no such function.
Any ideas?
I have an object which I need to reposition randomly.
I need to make sure I don’t position it inside parts of the landscape, which is also generated randomly.
OnTriggerNull would be nice, but there’s no such function.
Any ideas?
Choose a x and a z position randomly, then fire a ray from high up in y downwards. Place the object where the ray hits the landscape. (Assuming your world has y pointing upwards.)
You could pick a random X,Z location, and pick a Y location higher than anything in the landscape, and do a raycast down from there. Then place the object where the raycast hits.
Thanks Graham - I had thought of using Raycasting, but I need to check a cubic area, which means casting three rays minimum and which would still not be faultless, due to the angled geometry of the landscape. I want to make sure my Pickup object is not partially located in a wall, tree, rock etc.
Currently I am positioning the object and if it collides with something I reposition it until it doesn’t - which is a bit clunky. All this happens out of sight of the player, but I’d like to find a really elegant way of doing it. Neither this nor Raycasting is that tidy (would love OnTriggerNull - wouldn’t that be useful?)
Hmmmm…
The landscape is a cavern / tunnel with lots of obstructions. I just need a foolproof way of finding nice empty spots in it. So far can’t think of anything simpler than just randomly positioning the object repeatedly until OnTriggerEnter fails to flag a collision.
How about using an invisible mesh smaller than tunnels to specify areas you can spawn the object. You can use random coordinates from collider.bounds or generate random numbers and check if the collider contains them?
set object position and check distance raycast and call function its circle diagram
Use Physics.OverlapSphere to see if there is any object touching your object or not. Make the sphere a little bigger than your object if you want to be in a really open place. Also you can use SphereCast to cast from upward and see where it collides and put it there.