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

converting Physics2D.OverlapCircle to 3D

Discussion in 'Scripting' started by Caedriel, Dec 11, 2015.

  1. Caedriel

    Caedriel

    Joined:
    Jun 19, 2015
    Posts:
    8
    So i tried using overlap circle to create a torrent now i want it to be 3d , anyone knows how to convert it properly , I have attached my script and attempt with it .



    usingUnityEngine;
    usingSystem.Collections;
    publicclassTower : MonoBehaviour {

    publicGameObjectcannonprefab ;
    floatradius = 10.0f ;
    publicLayerMasklayermask ;
    publicfloatfireRate = 0.5F;
    privatefloatnextFire = 0.0F;
    //Usethisforinitialization
    voidStart ()
    {

    }
    //Updateiscalledonceperframe
    /*voidUpdate ()
    {
    Vector3position = gameObject.transform.position ;
    Collider2Dsomehting = Physics2D.OverlapCircle(position ,radius ,layermask );


    if (somehting != null && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Vector3Direction = somehting.transform.position - transform.position ;
    GameObjectcannon = Instantiate (cannonprefab , transform.position , Quaternion.identity) asGameObject ;
    cannon.GetComponent<Rigidbody2D>().AddForce(Direction.normalized*20.0f, ForceMode2D.Impulse);
    }
    }*/
    voidUpdate ()
    {
    Vector3position = gameObject.transform.position ;
    Collider [] somehting = Physics.OverlapSphere(position ,radius ,layermask );


    if (somehting != null && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Vector3Direction = somehting.transform.position - transform.position ;
    GameObjectcannon = Instantiate (cannonprefab , transform.position , Quaternion.identity) asGameObject ;
    cannon.GetComponent<Rigidbody2D>().AddForce(Direction.normalized*20.0f, ForceMode2D.Impulse);
    }
    }
    }
     

    Attached Files:

  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
  3. Caedriel

    Caedriel

    Joined:
    Jun 19, 2015
    Posts:
    8
    I just fixed that on my own after some time struggling , but thanks sose ! .