Search Unity

How would I have objects surround the player?

Discussion in 'Navigation' started by BearSheriff, Jul 11, 2015.

  1. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    Hey is there a way that I can have 5 objects surround a player and form a pentagon shape?
     
  2. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    483
    I think you're referring to objects following the player. If so then why not make them child objects of the player?
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    well, you know that a pentagram is made up of 5 points around a circle at 72 degrees.

    use that knowledge + trig to calculate the x/z positions.
     
  4. oswaldo-vix

    oswaldo-vix

    Joined:
    Aug 30, 2012
    Posts:
    6
    Try this.

    Code (CSharp):
    1.  
    2. public class Test : MonoBehaviour
    3. {
    4.     public GameObject target;
    5.     public GameObject cloneFrom;
    6.     public int amount;
    7.     public float radius;
    8.  
    9.     void Start()
    10.     {
    11.         Surround (target, cloneFrom, amount, radius);
    12.     }
    13.  
    14.     // this function replicate the object surrounding the target
    15.     public void Surround(GameObject target, GameObject prefab, int amount, float radius)
    16.     {
    17.         float angle = 360f / amount;
    18.  
    19.         for (int i = 0; i < amount; i++)
    20.         {
    21.             GameObject go = Instantiate (prefab) as GameObject;
    22.  
    23.             go.transform.Rotate (Vector3.up, angle*i);
    24.             go.transform.position = target.transform.position - (go.transform.forward * radius);
    25.            // OR  go.GetComponent<NavMeshAgent>().Warp(target.transform.position-(go.transform.forward*radius));
    26.         }
    27.     }
    28. }
    29.  
     
    Last edited: Aug 4, 2015