From 8a25f480aee132e4cd3ebafa9a210e82e0eee20e Mon Sep 17 00:00:00 2001 From: Sophie Date: Fri, 8 Apr 2016 02:33:23 -0400 Subject: [PATCH] random spawn around a circle --- Assets/Scripts/SpawnAsteroids.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/SpawnAsteroids.cs b/Assets/Scripts/SpawnAsteroids.cs index 524ca47..751e263 100644 --- a/Assets/Scripts/SpawnAsteroids.cs +++ b/Assets/Scripts/SpawnAsteroids.cs @@ -4,10 +4,16 @@ using System.Collections; public class SpawnAsteroids : MonoBehaviour { public GameObject myAsteroid; - public Vector3 initialPosition; + Vector3 center; + float x; + float y; + float d; // Use this for initialization - void Start () { + void Start() + { + center = new Vector3(0, 0); + d = 4; InvokeRepeating("Spawn", 0, 0.5F); } @@ -20,6 +26,16 @@ public class SpawnAsteroids : MonoBehaviour { void Spawn() { GameObject instance = Instantiate(myAsteroid); - instance.transform.position = new Vector3(10, 10, 0); + instance.transform.position = getPositions(); } + + Vector3 getPositions() + { + float theta = Random.Range(0F, 360F); + x = center.x - Mathf.Sin(theta) * d; + y = center.y - Mathf.Cos(theta) * d; + return new Vector3(x, y); + } + + }