random spawn around a circle

This commit is contained in:
Sophie 2016-04-08 02:33:23 -04:00
parent 8b8de8b58a
commit 8a25f480ae

View File

@ -4,10 +4,16 @@ using System.Collections;
public class SpawnAsteroids : MonoBehaviour { public class SpawnAsteroids : MonoBehaviour {
public GameObject myAsteroid; public GameObject myAsteroid;
public Vector3 initialPosition; Vector3 center;
float x;
float y;
float d;
// Use this for initialization // Use this for initialization
void Start () { void Start()
{
center = new Vector3(0, 0);
d = 4;
InvokeRepeating("Spawn", 0, 0.5F); InvokeRepeating("Spawn", 0, 0.5F);
} }
@ -20,6 +26,16 @@ public class SpawnAsteroids : MonoBehaviour {
void Spawn() void Spawn()
{ {
GameObject instance = Instantiate(myAsteroid); 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);
}
} }