- Home /
Stop deletion of the terrain object using Destroy.
I'm using
RaycastHit vHit = new RaycastHit();
Ray vRay = camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(vRay,out vHit,50))
{
Debug.Log(vHit.collider.name+" deleted.");
Destroy(vHit.collider.gameObject);
}
to delete objects in game and it works fine until you miss the object and delete the terrain, and fall to your doom.
Is there a smarter way of filtering out the terrain object than just throwing in an extra if statement checking for the name at every click?
I seem to have found an answer relating to tagging. Is comparing tags the best way?
Answer by Kiwasi · Jul 07, 2014 at 07:50 AM
Several ways to do it.
Tagging is probably the easiest to implement, simply use an if to check if the tag is the terrain. Trouble is you still calculated a RayCast against a mesh collider, which is kind of expensive
Using a layer mask to specify layers to ignore is likely the more efficient solution. Check out the documentation here and here.
Your answer
Follow this Question
Related Questions
Freezing on destruction of terrain. 1 Answer
Deform of terrain? (Real-time) 0 Answers
Tips to start underground digging 3D game 2 Answers
RTS game Any help about Hide mesh or destroy trees Terrain? 1 Answer
Click to Move problem with hills 3 Answers