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. Dismiss Notice

Beginner direction and magnitude question: CircleCollider2D

Discussion in 'Scripting' started by Dennooo, Jan 14, 2016.

  1. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    Hi there,

    I checked several threads on direction's and magnitude's but couldn't figure out the following:
    If I have a gameobject that solely carries a circlecollider2D, how can I get the position of a random point of the colliders boundary? I know that I can set random directions with random.range and set the vectors magnitude to the colliders radius, but I don't know how to do that properly in c#. :rolleyes:
     
  2. Play Creatively

    Play Creatively

    Joined:
    Sep 24, 2015
    Posts:
    6
    Code (CSharp):
    1.     CircleCollider2D CC2D;
    2.  
    3.     Vector3 randomPointInRadius
    4.     {
    5.         get
    6.         {
    7.  
    8.             float radius = CC2D.radius;
    9.             Vector2 center = CC2D.bounds.center;
    10.             Vector2 randomDirection = new Vector2(Random.Range(-1f,1f),Random.Range(-1f,1f)).normalized;
    11.             float randomLength = Random.Range(0,radius);
    12.             return center + randomDirection * randomLength;
    13.         }
    14.     }
    15.    
    16.     void Start ()
    17.     {
    18.         CC2D = GetComponent<CircleCollider2D>();
    19.     }
    Here, this is a pretty straight to the point calculation: using a random direction and a random length.
    Maybe you're not familiar with getter/setter's, "randomPointInRadius" is a Vector3 randomized every time you "get" it.

    So to move an object to a random spot within the CircleCollider just do this =
    ( gameObject.transform.position = randomPointInRadius )
    But remember that "randomPointInRadius " changes every time you use it.
     
    Dennooo likes this.
  3. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Could you explain more of what you are trying to do? aka why do you want to get a point on colliders boundary?
     
  4. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    78
    I have a circular "playfield" which is surrounded by another circle (a gameobject with a circlecollider2D) which represent the outer points of the players vision. The idea is, that I use the boundary of the circlecollider2D as random positions for enemy spawning.


    Thanks for the code! This was basically what I needed except, that I wanted the position to always be on the colliders boundary. I adjusted your code to get a random position on the boundary everytime the "spawn-object" is enabled:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnEnemies : MonoBehaviour {
    5.  
    6.     public GameObject enemyPrefab;
    7.  
    8.     public int amount;
    9.  
    10.     public GameObject spawnLocation;
    11.     private float spawnRadius;
    12.  
    13.     public float spawnWait; // time between each enemy
    14.  
    15.     public bool randomSpawnLocation;
    16.     public bool symmetricSpawnLocation;
    17.  
    18.     private Vector3 randomSpawnPoint;
    19.  
    20.  
    21.     void OnEnable()
    22.     {
    23.         spawnLocation = GameObject.Find("SpawnLocation");
    24.  
    25.         SetRandomLocation();
    26.  
    27.         StartCoroutine(Spawn());
    28.     }
    29.  
    30.  
    31.     public void SetRandomLocation()
    32.     {
    33.         Random.seed = System.Environment.TickCount;
    34.         float spawnRadius = spawnLocation.GetComponent<CircleCollider2D>().radius;
    35.         Vector2 center = spawnLocation.GetComponent<CircleCollider2D>().bounds.center;
    36.         Vector2 randomDirection = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized;
    37.         randomSpawnPoint = center + randomDirection * spawnRadius;
    38.     }
    39.  
    40.     IEnumerator Spawn()
    41.     {
    42.         if (randomSpawnLocation == true)
    43.         {
    44.             for (int i = 0; i < amount; i++)
    45.             {
    46.                 GameObject enemy = (GameObject)Instantiate(enemyPrefab, randomSpawnPoint, Quaternion.identity);
    47.                 yield return new WaitForSeconds(spawnWait);
    48.                 if (i == amount - 1)
    49.                 {
    50.                     Destroy(gameObject); // destroys the spawn when it finished spawning
    51.                 }
    52.             }
    53.         }
    54.      
    55.         if (symmetricSpawnLocation == true)
    56.         {
    57.             // logic for symmetric spawns
    58.         }
    59.  
    60.     }
    61.  
    62. }
    63.  
    There is another related problem: While the above code allows the spawning of all enemies at the same location, I want to implement symmetric spawning in the following sense:
    If the amount of enemies to spawn is for example four:
    Enemy 1 spawns at a random location on the boundary of the collider e.g. at center + Vector2(0f,1f)*spawnRadius, which is the right border;
    Enemy 2 spawns at the opposite direction, relative to enemy 1. Enemy 3 spawns at the top (or bottom) border, while enemy 4 spawns at the bottom (top) border. While this is easy for 4 enemies, Iam looking for a general way to allow for e.g. odd spawning numbers where e.g. 3 enemies spawn at different locations but remain the same distance to it's nearest partner.