Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active September 19, 2018 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/45d78a2a36ba34ed849718db14fb0eed to your computer and use it in GitHub Desktop.
Save davepape/45d78a2a36ba34ed849718db14fb0eed to your computer and use it in GitHub Desktop.
// Unity3d script to animate many circling objects
// Script creates the objects on startup, by instantiating a given object (create a prefab and assign it to the "obj" parameter in the inspector)
// Then moves them in a circle in the same way as circleChildren.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class createCirclingChildren : MonoBehaviour {
public int numberOfChildren = 1;
public Object obj;
public float speed = 1f;
public float radius = 1f;
// Use this for initialization
void Start () {
for (int i=0; i < numberOfChildren; i++)
Instantiate(obj, transform);
}
// Update is called once per frame
void Update () {
for (int i=0; i < transform.childCount; i++)
{
Transform child = transform.GetChild(i);
float angle = Time.time * speed + 2f*Mathf.PI*i/transform.childCount;
child.localPosition = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f) * radius;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment