- Home /
How to make a wavy circle?
Hello! I am trying to make a waved circle like the left one in the picture:
(I found this picture from http://www.kentchemistry.com/links/AtomicStructure/wavesElectrons.htm)
This is my attempt with LineRenderer:
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class Circle : MonoBehaviour {
public int pointsNum = 120;
public float radius = 2f;
public float angleVel = 0.1f;
public float speed = 0.5f;
public float amplitude = 0.05f;
public float rotateSpeed = 0.5f;
public float rotateRatio = 30f;
float startAngle = 0f;
float angle = 0f;
Vector3[] originalPos;
LineRenderer line;
int segments;
void Start()
{
line = gameObject.GetComponent<LineRenderer>();
segments = pointsNum - 1;
line.numPositions = pointsNum;
line.useWorldSpace = false;
originalPos = new Vector3[pointsNum];
CreatePoints();
}
void Update()
{
SimulateWave();
}
void CreatePoints()
{
float x;
float y;
float z = 0f;
float ang = 0f; // start from 0 degree
for (int i = 0; i < pointsNum; i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * ang) * radius;
y = Mathf.Cos(Mathf.Deg2Rad * ang) * radius;
line.SetPosition(i, new Vector3(x, y, z));
originalPos[i] = line.GetPosition(i);
ang += ((360f / segments) + (360f / segments) / pointsNum);
}
}
void SimulateWave()
{
// Each frame a new wave
startAngle += speed;
angle = startAngle;
// Loop through each point in the wave
for (int i = 0; i < pointsNum; i++)
{
float x = Mathf.Cos(angle) * amplitude;
float y = Mathf.Sin(angle) * amplitude;
float z = 0;
Vector3 wave = new Vector3(x, y, z);
line.SetPosition(i, originalPos[i] + wave);
angle += angleVel;
//angle += (totalAngle/pointsNum);
}
// Rotate the circle
Vector3 ro = new Vector3(0, 0, rotateRatio);
Quaternion roQ = Quaternion.Euler(ro);
transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation * roQ, rotateSpeed * Time.deltaTime);
Debug.Log(AudioData.amplitudeBuffer * rotateRatio);
}
}
But the first point's and the last point's position don't match. It looks like:
(Just like the right one in first picture)
I think that's because my wave is not in a full cycle maybe?
Really hope someone could provide some guides to make a wavy circle.
Thank you!
Your answer
Follow this Question
Related Questions
Programmatically specifying a random point in an arc in front of the player 1 Answer
Circular motion via the mathematical circle equation? 4 Answers
How to calculate inner vertices of a line renderer? (math question) 1 Answer
Find distance around a circle, from center point and another point 2 Answers
Converting screen cordinates to position on circle 2 Answers