Skip to content

Instantly share code, notes, and snippets.

@flycarl
Created February 24, 2017 07:59
Show Gist options
  • Save flycarl/3d8b02a9bcd9eb62160fe5f3bf657906 to your computer and use it in GitHub Desktop.
Save flycarl/3d8b02a9bcd9eb62160fe5f3bf657906 to your computer and use it in GitHub Desktop.
Unity Editor Script to Generate Objects in a curve
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class MakeCoinCurve : EditorWindow {
private Vector3 m_startPos;
private float m_forwardSpeed = 6f;
private float m_upSpeed = 13.5f;
private float m_gravity = 45f;
private List<Vector3> m_curvePts = new List<Vector3>();
[MenuItem("GameEditor/make coin curve")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(MakeCoinCurve));
}
void OnGUI()
{
EditorGUILayout.BeginVertical();
m_startPos = EditorGUILayout.Vector3Field("start pos", m_startPos);
m_forwardSpeed = EditorGUILayout.FloatField("forward speed: ", m_forwardSpeed);
m_upSpeed = EditorGUILayout.FloatField("up speed: ", m_upSpeed);
m_gravity = EditorGUILayout.FloatField("gravity: ", m_gravity);
EditorGUILayout.Space();
if (GUILayout.Button("make selections curve", GUILayout.Height(20)))
{
makeCurvePts();
float totallength = totalLength();
makeCurveEqualLength(Selection.gameObjects, totallength);
}
if (GUILayout.Button("set Start Position", GUILayout.Height(20)))
{
setStartPos(Selection.gameObjects);
}
EditorGUILayout.EndVertical();
}
void makeCurve(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
if(m_gravity == 0)
{
Debug.LogError("gravity == 0");
return;
}
if(count < 2)
return;
float totalt = 2 * m_upSpeed/m_gravity;
float t = 0;
Vector3 pos = Vector3.zero;
for (int i = 0; i < count; i++)
{
t = totalt * i/(count - 1);
GameObject obj = gameObjcects[i];
pos.x = m_forwardSpeed * t;
pos.y = m_upSpeed * t - m_gravity * t * t/2;
pos.z = 0;
obj.transform.localPosition = pos;
}
}
void makeCurvePts()
{
m_curvePts.Clear();
float step = 0.01f;
float totalt = 2 * m_upSpeed / m_gravity;
int count = Mathf.CeilToInt( totalt / step);
float t = 0;
Vector3 pos = Vector3.zero;
for (int i = 0; i < count; i++)
{
t = totalt * i / (count - 1);
pos.x = m_forwardSpeed * t;
pos.y = m_upSpeed * t - m_gravity * t * t / 2;
pos.z = 0;
m_curvePts.Add(pos);
}
}
float totalLength()
{
float length = 0f;
for (int i = 1; i < m_curvePts.Count; i++)
{
Vector3 seg = m_curvePts[i] - m_curvePts[i - 1];
length += seg.magnitude;
}
return length;
}
void makeCurveEqualLength (GameObject[] gameObjcects, float totalLength)
{
int count = gameObjcects.Length;
if (m_gravity == 0)
{
Debug.LogError("gravity == 0");
return;
}
if (count < 2)
return;
float averageLen = totalLength / (count-1);
GameObject obj = gameObjcects[0];
obj.transform.localPosition = Vector3.zero;
int j = 1;
float length = 0f;
for (int i = 1; i < m_curvePts.Count; i++)
{
Vector3 seg = m_curvePts[i] - m_curvePts[i - 1];
length += seg.magnitude;
if (length > averageLen)
{
obj = gameObjcects[j];
obj.transform.localPosition = m_curvePts[i];
j++;
length = 0;
if(j == gameObjcects.Length -1)
{
obj = gameObjcects[j];
obj.transform.localPosition = m_curvePts[m_curvePts.Count - 1];
}
}
}
for (int i = 0; i < gameObjcects.Length; i++)
{
obj = gameObjcects[i];
obj.transform.localPosition = obj.transform.localPosition + m_startPos;
}
}
void setStartPos(GameObject[] gameObjcects)
{
int count = gameObjcects.Length;
Vector3 minXPos = Vector3.zero;
if (count > 0)
minXPos = gameObjcects[0].transform.localPosition;
for (int i = 0; i < count; i++)
{
GameObject obj = gameObjcects[i];
if (obj.transform.localPosition.x < minXPos.x)
minXPos = gameObjcects[i].transform.localPosition;
}
m_startPos = minXPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment