I am working on a 3D game wherein which if the bird goes through a pipe successfully it scores a point or else it is dead.I am able to implement this very much.
Also I am spawning the pipe (which is a pre-fab)through a script. The position of the spawned pipe is in a random position in front of the bird.The code for that is as follows.This code is written in the GameControl script which takes care of the state of the game.
public class GameControl : MonoBehaviour
{
public GameObject pipe;
public static GameControl instance;
..........
..........
..........
void Update()
{
..........
..........
..........
spawnXposition = CurrentXpositionOfBird + Random.Range(-1, 1);
spawnYposition = CurrentYpositionOfBird + Random.Range(-1, 1);
spawnZposition = CurrentZpositionOfBird + Random.Range(50, 100);
SpawnPipe(spawnXposition, spawnYposition, spawnZposition);
}
..........
..........
..........
public void SpawnPipe(float x, float y, float z)
{
pipe.transform.position = new Vector3(x, y, z);
CurrentZpositionOfScoringCollider = pipe.transform.position.z + 2.62f;
}
}
Where SpawnPipe function is taking care of the part which is to create the pipe.I have attached pipe prefab to the pipe GameObject in the inspector.
Now here is the problem.When I spawn the pipe I don't want it to touch any part of the terrain. If it ever touches I want to respawn until I get the pipe in such a location that it is not touching the terrain. For that in the pipe prefab I have attached a script and in the OnCollisionEnter function I have destroyed the pipe. Here is the code below:
private void OnCollisionEnter(Collision collision)
{
Destroy(GameControl.instance.pipe);
}
The logic above is if the spawned pipe touches with the terrain, destroy it.In that case when the spawned pipe is destroyed the update function runs again to spawn a new pipe.As long as it is not colliding with the terrain,it will be spawned again and again.
But I am still able to see the pipe touching in fact ,nicely merging with the terrain. For terrain I have it is added a mesh collider and have made it a rigid body with isKinematic checked. It works perfectly when the bird hits the terrain and I am able to do display the game over message
For the pipe I have added 3 box colliders ,one at the bottom of the pipe and the other two at the sides of the pipe.When it collides with the bird,it is succesfully displaying Game Over message. But the coillision between the pipe and terrain is not getting detected .I dont know where I am going wrong.Please help. I am pretty new to Unity as well
<<<----------------------EDIT----------------------->
The link for the FBX model of the terrain
https://docs.google.com/uc?export=download&id=1kFUtPpQnopa7489i-iQwC2qEiQECbWCu