I’m trying to code a procedural corridor generator, there are 3 types of corridor, 1 is the straight one, 2 is the right turn one and 3 is the left turn one.
I placed 2 Empty GameObjects as Parent and Child respectively of the corridor, the parent one is the fixed axis, and the child one is the spawnpoint of the new corridor.
I’ve run into a problem, when it generates the corridors, somethimes they intersect, so to solve this I tried writing a code that checks if the GameObject is colliding with another GameObject, if the corridor is colliding destroy it and retry creating another one, until it finds a solution; I tried both OnCollisionEnter and OnTriggerEnter and nothing, it doesn’t check, I tried making a Debug.Log but it’s not writing anything in the Console.
Here is the code to generate the corridors:
using UnityEngine;
using System.Collections;
public class ProceduralObjects : MonoBehaviour {
private GameObject[] corridorTypes = new GameObject[3];
private int randomSpawn;
public static bool generate = false;
public GameObject spawnPoint;
private bool instantiate1Time = true;
// Use this for initialization
void Start () {
GameObject corridorType1 = (GameObject)Resources.Load("Corridoio1Spawner", typeof(GameObject));
GameObject corridorType2 = (GameObject)Resources.Load("Corridoio2Spawner", typeof(GameObject));
GameObject corridorType3 = (GameObject)Resources.Load("Corridoio3Spawner", typeof(GameObject));
corridorTypes[0] = corridorType1;
corridorTypes[1] = corridorType2;
corridorTypes[2] = corridorType3;
}
// Update is called once per frame
void Update () {
randomSpawn = Random.Range(0, corridorTypes.Length);
if(generate && instantiate1Time){
Instantiate(corridorTypes[(randomSpawn)], spawnPoint.transform.position, spawnPoint.transform.rotation);
StartGame.temporaryCorridorGenerated += 1;
instantiate1Time = false;
}
}
void CollisionWithOtherWall(){
StartGame.temporaryCorridorGenerated -= 1;
Destroy(gameObject);
}
}
And here is the code to send the message to destroy the GameObject (This script is attached to the child of the Empty GameObject):
using UnityEngine;
using System.Collections;
public class ProceduralObjectsChild : MonoBehaviour {
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "Wall"){
SendMessageUpwards("CollisionWithOtherWall", SendMessageOptions.DontRequireReceiver);
Debug.Log("Collision With Other Wall");
}
}
}
The problem, I think, is that Unity is not checking if the object, right after being instantiated, is colliding or not.