- Home /
Radial object spawning like arrow fest game?
Hi,
I am creating a game that kind of requires the mechanic of arrows in the following game arrow fest (https://www.youtube.com/watch?v=7njziDe4mts). I can't seem to figure out how to do that arrow grouping and circling? I thought of spawning objects around player in circles and I used the following code
void UpdateCircle()
{
for (int i = 0; i < instantiateList.Count; i++)
{
float angle = i * Mathf.PI * 2f / instantiateList.Count;
Vector3 newPos = new Vector3( Mathf.Cos(angle) * radius , Mathf.Sin(angle) * radius , 0 );
instantiateList[i].transform.localPosition = newPos;
}
}
But it didn't work as I wanted and in the video. I wanted to ask if there is a better way to achieve such seamless grouping of objects around the player? Any ideas, tips, resources will be appreciated.
What about it didn't work as you wanted? Can you share a screenshot?
I was able to achieve something similar to what I want, but it is too symmetrical.
public void spawn(float radius, float numberofspawns)
{
float archLength = (degree / 360) * 2 * Mathf.PI;
float nextAngle = archLength / numberofspawns;
float angle = 0;
GameObject g = new GameObject();
g.transform.localPosition = Vector3.zero;
for (int i = 0; i < numberofspawns; i++)
{
float x = Mathf.Cos(angle) * radius * direction;
float y = Mathf.Sin(angle) * radius * direction;
GameObject obj = Instantiate(Resources.Load("Sphere") as GameObject, transform.position, Quaternion.identity);
obj.transform.localPosition = new Vector3(x, y, transform.position.z);
obj.transform.SetParent(g.transform);
angle += nextAngle;
}
g.transform.SetParent(transform);
g.transform.localPosition = Vector3.zero;
}
Answer by andrew-lukasik · Sep 23, 2021 at 09:04 AM
http://algorithmicbotany.org/papers/abop/abop-ch4.pdf
using UnityEngine;
public class PhyllotaxisTest : MonoBehaviour
{
[SerializeField][Min(0)] int _count = 256;
[SerializeField] float _c = 5.25f;
[SerializeField] float _radiusScale = 0.0001f;
[SerializeField] float _gizmoSize = 0.075f;
void OnDrawGizmos ()
{
// reference: http://algorithmicbotany.org/papers/abop/abop-ch4.pdf (page 2)
for( int n=1 ; n<=_count ; n++ )
{
float phi = _c * Mathf.Sqrt(n);
float r = n * 137.5f * _radiusScale;
Vector3 point = new Vector3( Mathf.Cos(phi)*r , Mathf.Sin(phi)*r , 0 );
Gizmos.DrawSphere( point , _gizmoSize );
}
}
}
Change _c
to produce all kind of cool patterns. 5.25f
is very bland one at that.
Hey, Thank you so much for your answer and the article, This actually looks really cool and close to what I am ai$$anonymous$$g for at the moment. I created something similar but it was really symmetric.
using this code
public void spawn(float radius, float numberofspawns)
{
float archLength = (degree / 360) * 2 * Mathf.PI;
float nextAngle = archLength / numberofspawns;
float angle = 0;
GameObject g = new GameObject();
g.transform.localPosition = Vector3.zero;
for (int i = 0; i < numberofspawns; i++)
{
float x = Mathf.Cos(angle) * radius * direction;
float y = Mathf.Sin(angle) * radius * direction;
GameObject obj = Instantiate(Resources.Load("Sphere") as GameObject, transform.position, Quaternion.identity);
obj.transform.localPosition = new Vector3(x, y, transform.position.z);
obj.transform.SetParent(g.transform);
angle += nextAngle;
}
g.transform.SetParent(transform);
g.transform.localPosition = Vector3.zero;
}
I think your solution will add that asymmetry that I am looking for which will help with the effect.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Instantiate object in C# 1 Answer
Destroying an instantiated object colliding with another instantiated object 3 Answers
"NullReferenceException: Object reference not set to an instance of an object" with instantiate C# 1 Answer