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

Have multiple enemies keep distance from one another

Discussion in 'Scripting' started by theZiggy1, Mar 2, 2020.

  1. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    Hi guys,

    so I'm trying to have a group of enemies maintain distance from one another,until one is given permission to fight the player. I also want them to 'circle' around the player, while waiting for their turn. Think old dubbed action movies.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Put them on a circle around the player, and space them evenly by Phi (divide 2*Pi by the number of enemies). If you NNeed to have minimal Separation between the enemies, from one to the next, remember that the distance to the next Player is almost equal to distance = sin(Phi) * radius, and solve for radius

    Edit:
    I got that wrong: the mindistance between Players c = 2*radius * sin(phi/2). Solve for radius.
     
  3. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    So how to put them in a circle, in this implementation? clampmagnitude?
    seperation isnt an issue, if i have 3 enemies, they can all be evenly spaced around the player.

    so my code would be

    Distance = sin( 2* Pi/numEnemies) * radius;

    where radius is how far away i want them? How to get this as a vector/location
     
  4. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14


    im still a little lost how to get the object to here, as a location from the player, tbh.

    should i not be able to set the radius as a variable of my choice?

    or am i thinking about this backwards?
     
  5. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    For a point on a circle:
    https://math.stackexchange.com/questions/260096/find-the-coordinates-of-a-point-on-a-circle
    if you have 3 enemies then your theta will be 360 / 3 :) and so on..
    I think the final formula of theta will be
    Code (CSharp):
    1.  
    2. var theta = 0f;
    3. offset += Time.deltatime * speed; // if you want to move the enemies
    4. for (int i = 0; i < NumberOfEnemies; i++)
    5. {
    6.   theta += offset + (360 / NumberOfEnemies) * (i + 1);//later edit
    7.   //here place your enemy with formula from link
    8. }
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The distance between the enemies (when spaced around a circle evenly by the same angle) is defined by the radius. Think about spokes in a wheel. If you need more distance between the spokes, you'll have to out further (increase the radius) until the are fare enough apart.
     
  7. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    okay cool thanks, this does make sense now. i was thinking about this backwards.