First of all English is not my native language.
And I am a newcomer
My problem is: At runtime, gameobjects that collide with each other are pushed away by the physics engine to ensure that there is no collision. Is there a way to know that no collisions have occurred after the physics engine is applied, and at runtime?
Project information that might come in handy. I am designing a procedural generated map script, my running process is based on : __Programming recent news | Game Developer
The author’s .gif picture is also attached as an example
I want to know if there is a physics engine API that can view collision events in the entire world.
public class Room : MonoBehaviour
{
//Settings to have the detection efficient
[SerializeField]
private float checkDelay = 0.5f;
[SerializeField]
private int checkLimit = 100;
private int currentCheckLimit = 0;
//Settings for the OverlapBoxCast
public Vector2 exampleRoomSize = new Vector2(10, 30);
public Vector2 roomCenterPointWorldCoordinates = new Vector2(5, 10);
public LayerMask roomCollidersLayerMask;
private bool isColliding;
public bool IsColliding
{
get { return isColliding; }
private set { isColliding = value; }
}
//When the room is created start checking for collisions
private void Start()
{
StartCoroutine(CheckCollisionCOroutine());
}
/// <summary>
/// Coroutine that checks for collisions with other rooms
/// </summary>
/// <returns></returns>
private IEnumerator CheckCollisionCOroutine()
{
yield return new WaitForSeconds(checkDelay);
IsColliding = CheckCollision();
if (IsColliding == true && currentCheckLimit < checkLimit)
StartCoroutine(CheckCollisionCOroutine());
}
/// <summary>
/// Methods that uses OverlapBox to cast a boxcast and detect if there are any collisions
/// </summary>
/// <returns></returns>
public bool CheckCollision()
{
return Physics2D.OverlapBox(roomCenterPointWorldCoordinates, exampleRoomSize, 0, roomCollidersLayerMask) == null;
}
}
Basically what it does is checks every 0.5 second if there is a collisions or not. After 100 checks if the collision is still there it gives up on this room.
Again it seems like a pretty advanced tutorial. If you are interested in the procedural 2d dungeon generation maybe you will find my tutorial useful: