- Home /
Detect on instantiate if object is instantiated in terrain
Hi, I have an object with a box collider which I instantiate where I click on my terrain. Everything works fine except that it's instantiating in the mountains. I have added a box collider on the object but I don't know where to go from here to make it not instantiate when the box collider is colliding with my terrain.
Thanks in advance!
Answer by YoungDeveloper · Oct 12, 2013 at 10:37 PM
You instantiate it from raycast hit is it true ? If so, just add some value to y axis.
Vector3 spawnPoint = hit;
spawnPoint.y += 3f; //or any value
//Now just use spawnpoint as Instantiate position (2 argument)
It's true, but I described it a bit wrong, it's not instantiating IN the ground literllay, it's clipping in when instantiation on the mountain. I want to check that if you can't see the whole object (if the object is a bit inside the terrain), don't instantiate. If that made any sense.
Okay, are you instantiating in editor mode or in-game ?
You can check if object is colliding with something for one time, or use this:
http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html
Then if object collides, just destroy it
I have setup this example, but how do I get the output?
void AddDamage() {
Debug.Log ("???");
}
void TerrainCollide(Vector3 center, float radius) {
Collider[] hitColliders = Physics.OverlapSphere(Vector3.zero, 50);
int i = 0;
while (i < hitColliders.Length) {
hitColliders[i].Send$$anonymous$$essage("AddDamage");
Debug.Log ("Collider hit.");
i++;
}
}
Your answer
Follow this Question
Related Questions
Solidifying Trees From Terrian Tool 1 Answer
Randomly some prefab instances have Character controller misaligned 0 Answers
Terrain with lots of objects 1 Answer
Spawning blood splatter at the point the bullet hit a collider? 1 Answer
Check if an Instantiated object is colliding with another object right after being instantiated 1 Answer