How to make a Sine wave generated from a unit circle with dynamic frequency and amplitude?
I want to create a Sine Wave generated from a unit circle in Unity whose amplitude and frequency can be dynamically changed by changing the speed and radius of the circle. Ex:
The result should look something like this(see pictures below) where one continuous curve shows the sustained changes in frequency and amplitude when looked over time:
I looked at the Unity Asset Store, there are a few wave generators, but none of them work with varying amplitude and frequency such that the resulting curve is one continuous curve where previous amplitudes and frequency still show in the editor.
Answer by Hellium · Feb 22, 2018 at 09:00 AM
Here is a very simple example: In the editor, you can define a curve to change the frequency and amplitude over time.
Create a cube and attach a Line Renderer to visualize the resulting sine curve
Attach the following
Sine
scriptChange the values shown in the inspector.
I have hard time with the frequency, but with an amplitude curve with the keys {0,1}, {5,5}, {10,1}, I had the 1st sine curve you've linked.
using UnityEngine;
using System.Collections;
public class Sine : MonoBehaviour
{
[SerializeField]
private AnimationCurve frequencyCurve;
[SerializeField]
private AnimationCurve amplitudeCurve;
[SerializeField]
private float xSpeed = 1;
private void Start()
{
frequencyCurve.postWrapMode = WrapMode.Loop;
amplitudeCurve.postWrapMode = WrapMode.Loop;
}
private void Update()
{
float t = Time.time;
float amplitude = amplitudeCurve.Evaluate( t );
float frequency = frequencyCurve.Evaluate( t );
float phase = 0;
float y = amplitude * Mathf.Sin( 2 * Mathf.PI * frequency * t + phase );
transform.position = Vector3.up * y + Vector3.right * xSpeed * t;
}
}
This way I'm able to emulate the motion but only to a limited capacity. Does not really serve as a solution as it does not respond to dynamic changes on the unit circle from where this sine wave is supposed to be generated.