IsTouching() not working right after Colliders2D movement/instanciation
Hi
I got a problem with Collider2D collisions. I'm actually working on the prodecural generation of map made of different rooms. To make sure rooms aren't colliding, I attached to each of them a Collider2D.
.
Problem is : Right after moving/instanciating a Collider2D and on the same method I did (=same frame), I can't use IsTouching() method with that collider. It always returns false.
.
I read that the colliders are not updated and I need a physical update. I tried playing with Physics.autoSyncTransforms(), Physics.SyncTransforms() and their 2D versions but it won't work. OnTriggerEnter2D and OnCollisionEnter2D won't work because all happens in a single method call (= single frame). Note that waiting for the next FixedUpdate is not a solution because I don't want to wait 2 hours for my program to finish generating.
.
Here is an example of my problem. In Start(), IsTouching() returns false. In Update(), it returns True (so colliders are working).
public Collider2D c1;
public Collider2D c2;
void Start() {
//Creating and setting up colliders2D
Collider2D c1 = transform.Find("A").gameObject.AddComponent<BoxCollider2D>();
c1.isTrigger = true;
Collider2D c2 = transform.Find("B").gameObject.AddComponent<BoxCollider2D>();
//Creating Rigidbody to one of them
Rigidbody2D rb = transform.Find("B").gameObject.AddComponent<Rigidbody2D>();
rb.isKinematic = true;
rb.simulated = true;
Debug.LogError("test : " + c1.IsTouching(c2));//False
}
void Update()
{
Debug.LogError("test : " + c1.IsTouching(c2));//True
}
Please help :'(
Your answer
Follow this Question
Related Questions
How can I make an object moving on its own bounce off walls 1 Answer
Realistic Soccer VR 2 Answers
Colliders "offset" and going down too. 0 Answers
collision is making its own vector 1 Answer
How do I count Hits ?? 2 Answers