- Home /
Check if an Instantiated object is colliding with another object right after being instantiated
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.
Answer by robertbu · Dec 21, 2013 at 05:54 PM
I think, is that Unity is not checking if the object, right after being instantiated, is colliding or not.
Correct. You have to wait a frame to get your collision. Not a problem I personally have had to solve, but the typical solutions posted on this list include keeping the object invisible and waiting a frame to see if there is a collision. Or you can use Physics.CheckSphere() or Physics.OverlapSphere() (which both will return some false positives). Or you can use some sort of casting such as Physics.Raycast(). Or knowing the physical properties of your objects, calculate the collision yourself.
Okay I got it, but it's the same thing, it's not doing anything;
I looked up in Google how to wait a frame, and ended up with this code:
public class ProceduralObjectsChild : $$anonymous$$onoBehaviour {
GameObject other2;
void OnTriggerEnter(Collider other){
other2 = other.gameObject;
StartCoroutine("Wait1Frame");
}
IEnumerator Wait1Frame(){
yield return 0;
if(other2.gameObject.tag == "Wall"){
Send$$anonymous$$essageUpwards("CollisionWithOtherWall", Send$$anonymous$$essageOptions.DontRequireReceiver);
Debug.Log("Collision With Other Wall");
}
}
}
As mentioned, it's not something I've done before. So I played for a few $$anonymous$$utes. The solution I came up with is have the collider on the object you are placing have 'IsTrigger' set to true (you can change it later). Then this code worked:
using UnityEngine;
using System.Collections;
public class Bug25c : $$anonymous$$onoBehaviour {
public bool collided = false;
void Start() {
StartCoroutine(CheckForCollision());
}
void OnTriggerEnter() {
collided = true;
}
IEnumerator CheckForCollision() {
yield return null;
Debug.Log (collided);
}
}
Note OnCollisionEnter() worked, but then you get the physics interaction between the two objects.
Okay, that pretty much worked, now I got to find out why the objects are deleting themselves at each frame, just creating and deleting, creating and deleting, weird, maybe they are colliding with each other, thanks for the Code!
Your answer
Follow this Question
Related Questions
OnTriggerEnter not working 0 Answers
WTF is wrong! OnTrigger, OnCollision, nothing working! 2 Answers
splatPrototypes not changing in exported game 0 Answers