- Home /
Spawn prefabs in a circle?
I found this http://answers.unity3d.com/questions/170413/position-objects-around-other-object-forming-a-cir.html but the example given is in UnityScript and I've had trouble converting it to C#.
Is there a better way of doing the following:
1) Spawn enemies along a circular perimeter around a Vector3 point.
2) Vary the distance from the point.
If that is the best way, could someone help convert it to C#? Here it is again:
function RandomCircle(center:Vector3, radius:float): Vector3 {
// create random angle between 0 to 360 degrees
var ang = Random.value * 360;
var pos: Vector3;
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;
}
// How to use:
var center = transform.position;
for (i = 0; i < numObjects; i++){
var pos = RandomCircle(center, 10);
// make the object face the center
var rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
Instantiate(prefab, pos, rot);
}
This code looks fine. It uses a polar to rect conversion. An alternate approach (a bit more typical of Unity code) would be to use a Vector rotated around a point. Post your attempt at a C# conversion, and I'm sure we can help you over any rough spots. I see nothing out of the ordinary in the conversion.
Vector3 RandomCircle ( Vector3 center , float radius ){
Vector3 ang = Random.value * 360;
Vector3 pos;
pos.x = center.x + radius * $$anonymous$$athf.Sin(ang * $$anonymous$$athf.Deg2Rad);
pos.y = center.y + radius * $$anonymous$$athf.Cos(ang * $$anonymous$$athf.Deg2Rad);
pos.z = center.z;
return pos;
}
Vector3 center = transform.position;
for (i = 0; i < numObjects; i++){
Vector3 pos = RandomCircle(center, 10);
Vector3 rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
Instantiate(prefab, pos, rot);
}
I get "parsing error" and "Class, struct, or interface method must have a return type".
Answer by robertbu · May 26, 2014 at 02:36 AM
Here is the completed C# translation. You had the type wrong on line 2 and line 13. You were missing an 'int' on line 11.
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;
}
}
Great start. Now I just need to randomize the radius and I'll have what I've been looking for for my project too. :)
There is some error when an object is instantiated at Vector3(0,0,AnyNegativeZValue), it will flip around 180° on its Z-Axis, e.g. the car is upside down.
You shouldn't use Quaternion.FromToRotation
in such a case. Use Quaternion.LookRotation(pos-center)
i don't know if this is possible but the prefabs spawn far away from the object that they are meant to spawn on and i would love to make them spawn closer if you know how plz tell me
Answer by CaptainTR · Nov 11, 2015 at 02:51 PM
try this code
int numObjects = 12;
public GameObject prefab;
void Start()
{
Vector3 center = transform.position;
for (int i = 0; i < numObjects; i++)
{
int a = i * 30;
Vector3 pos = RandomCircle(center, 1.0f ,a);
Instantiate(prefab, pos, Quaternion.identity);
}
}
Vector3 RandomCircle(Vector3 center, float radius,int a)
{
Debug.Log(a);
float ang = a;
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;
}
usually your plane will be 0,0,0 on all transforms to make it look right top down you would want to swap y/z:
pos.z = center.z + radius * $$anonymous$$athf.Cos(ang * $$anonymous$$athf.Deg2Rad);
pos.y = center.y;
You can replace
int a = i * 30;
with
int a = 360 / numObjects * i
which wil make a bit more dynamic.
how to make it using image? i had made like above . prefab = image. but when i try, the prefab out of canvas? when i try using 2d sprites, it's work like above.
Answer by tesan · Aug 09, 2017 at 07:33 PM
you can watch this plugin: http://u3d.as/Un7, I think it's what you want