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

2D Spawning in Half Circle

Discussion in '2D' started by _jacks, Sep 18, 2015.

  1. _jacks

    _jacks

    Joined:
    Nov 27, 2014
    Posts:
    27
    Hello

    So I'm not good at all at math but I'm trying to Instantiate a prefab in a half circle

    eg. Spawning randomly on the X axis in 180 degrees eg. the top half of a circle

    I have no idea how to do it other than using Sin/Cos but I don't know the theory behind them or how it should look code wise so I was hoping for someone to provide the correct math or code but I would also like to understand what is actually going on

    Thanks
     
  2. CodeGorilla

    CodeGorilla

    Joined:
    Sep 15, 2015
    Posts:
    17
  3. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    I did something similar, but with a full circle:
    UnityEngine Random insideUnitCircle normalized * radius;
    This will take a random position inside a circle and then normalize it (so that the distance to the center is always the same). Then multiply that random position with radius to get a circle of the size you want.

    The only difference between my code and yours, is that you will have to do an extra if check. For instance:
    Vector3 position;
    do{ position = UnityEngine Random insideUnitCircle normalized * radius }
    while(position.y > 0)

    And then possibly set position.z to 0;
     
  4. _jacks

    _jacks

    Joined:
    Nov 27, 2014
    Posts:
    27
    Thanks guys