Skip to content

Instantly share code, notes, and snippets.

@arkarmintun1
Created March 18, 2020 14:59
Show Gist options
  • Save arkarmintun1/a08346717c046cf1d579e081b87453ff to your computer and use it in GitHub Desktop.
Save arkarmintun1/a08346717c046cf1d579e081b87453ff to your computer and use it in GitHub Desktop.
Spawn Game Objects In Circle
using UnityEngine;
using System.Collections;
public class SpawnInCircle : MonoBehaviour {
public int numObjects = 5;
public GameObject prefab;
void Start() {
Vector3 center = transform.position;
for (int i = 0; i < numObjects; i++){
Vector3 pos = RandomCircle(center, 5.0f);
Quaternion rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
Instantiate(prefab, pos, rot);
}
}
Vector3 RandomCircle (Vector3 center, float radius){
float ang = Random.value * 360;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.z = center.z;
return pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment