Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics.OverlapSphere's 2D equivalent?

Discussion in '2D' started by Aidy, Feb 18, 2015.

  1. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    Hi,

    Is there an equivalent to Physics.OverlapSphere from the 3D physics in the 2D physics engine?

    I'm trying to return it into an array, which OverlapSphere will let me, but OverlapCircle won't. Should I just raycast instead, or is there an easier way to do this that I'm just missing? I haven't used 2D physics before, so I've no idea.

    Code (CSharp):
    1. //        Vector3 facingDirection = Vector3.right;
    2. ////        Collider [] enemyColliders = Physics.OverlapSphere (myTransform.position + facingDirection * 1, 1, 1 << 8);            //3D version that returns into an array
    3. //
    4. //        Collider2D [] enemyColliders = Physics2D.OverlapCircle (myTransform.position + facingDirection * 1, 1, 1 << 8);            //2D version, cannot return into an array, perhaps use a raycast?
    5. //
    6. //        //as long as there are enemies existing in the game then they can be damaged, prevents enemies becoming invincible once they spawn due to being prefabs
    7. //        for (int i =0; i < enemyColliders.Length; ++i)                                  
    8. //        {
    9. //            Debug.Log(i);
    10. //            enemyColliders[i].GetComponent<EnemyScript>().TakeDamage(1);
    11. //        }
     
  2. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
  3. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19