Created
March 18, 2020 14:59
-
-
Save arkarmintun1/a08346717c046cf1d579e081b87453ff to your computer and use it in GitHub Desktop.
Spawn Game Objects In Circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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