Skip to content

Instantly share code, notes, and snippets.

@Demkeys
Created April 16, 2018 11:48
Show Gist options
  • Save Demkeys/788a7e253cb2e47141cd7b5626401568 to your computer and use it in GitHub Desktop.
Save Demkeys/788a7e253cb2e47141cd7b5626401568 to your computer and use it in GitHub Desktop.
Simple script that procedurally generates a circle of spheres, and then updates the position of those spheres every frame to form patterns.
// Just something fun I came up with when learning Trigonometry.
// Attach this script to an empty gameobject and enter Play Mode. A circle of spheres will be created. All the spheres will be
// children of the gameobject that this script is attached to. When you hit the Spacebar key, the position of each sphere will
// start updating and from that point on, every frame the positions will be updated to form patterns.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircleGenScript01 : MonoBehaviour {
public float someAngle = 0f;
public float posMultiplier = 5f;
public float incValue = 10f;
public List<GameObject> spheresList;
public List<float> anglesList;
public bool updateSpheres = false;
// Use this for initialization
void Start () {
CreateSpheres();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) updateSpheres = true;
if(updateSpheres) UpdateSpheres();
}
void CreateSpheres()
{
spheresList = new List<GameObject>();
anglesList = new List<float>();
while(someAngle < 360)
{
float xPos = Mathf.Cos(someAngle * Mathf.Deg2Rad) * posMultiplier;
float yPos = Mathf.Sin(someAngle * Mathf.Deg2Rad) * posMultiplier;
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.parent = transform;
go.transform.localPosition = new Vector3(xPos,yPos,0f);
spheresList.Add(go);
anglesList.Add(someAngle);
someAngle += incValue;
}
}
void UpdateSpheres()
{
for(int i = 0; i < spheresList.Count; i++)
{
float xPos = Mathf.Cos(anglesList[i] * Mathf.Deg2Rad);
xPos *= Mathf.PerlinNoise(0, Time.time * 0.5f) * posMultiplier;
float yPos = Mathf.Sin(anglesList[i] * Mathf.Deg2Rad);
yPos *= Mathf.PerlinNoise(0, Time.time * 0.5f) * posMultiplier;
float someVal = Mathf.PerlinNoise(xPos, yPos) * 2.5f;
spheresList[i].transform.localPosition = new Vector3(
xPos * someVal, yPos * someVal, spheresList[i].transform.localPosition.z
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment