- Home /
Spawn Objects around a sphere
i want a script that spawn objects around a sphere
Hi @SalahChafaiGameDesigner
Your question is pretty vague.
How do you want your objects spawned? On random locations or in some specific patterns perhaps?
You say you need some free advice, but you don't pretty much even define what you need.
Answer by dan_wipf · Aug 29, 2018 at 11:04 AM
http://bfy.tw/JdB6 have a look at the link...
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public int numObjects = 10;
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;
}
}
@dan_wipf - plus 1 for the link. Good percent of questions here are lmgtfy category. In this day and age, I wonder how search engine isn't obvious first place to everyone when you have a question.
that’s so true! alot of questions are com$$anonymous$$g up too much in a short period, and are answered a hundred times..
100%. Google is like having a friend that has all of human knowledge in his head and all you need to do is ask them the right question and you always get an instant reply.
The fact that people obviously dont use it is terrifying to me. In some forums just to make the point I have often just copied and pasted their question into google and linked to or quoted the literal first search result to show them how silly it is.
I'll add a variant - without sin and cos (and transform position should be added to final position, it's missing here):
public SphereCollider sphereCol;
public GameObject spawned;
void Start ()
{
var r = sphereCol.radius;
for (int i = 0; i < 100; i++)
{
var spawned = Instantiate(this.spawned) as GameObject;
var x = Random.Range(-1f,1f);
var y = Random.Range(-1f,1f);
var z = Random.Range(-1f,1f);
var vec = new Vector3(x,y,z).normalized * r;
spawned.transform.position = vec;
}
}
is there a method to make it so they each spawn maybe 1 by 1 every 5 or so seconds?