我想创建一个从Unity中的单位圆生成的正弦波,其幅度和频率可以通过改变圆的速度和半径来动态改变。例如:
结果应如下所示(见下图),其中一条连续曲线显示了随时间变化的频率和幅度的持续变化:
这就是我目前正在接近产生正弦波的问题(这个脚本现在没有链接到圆圈),但它不允许旧值持续超时。< / p>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class generateSineWave : MonoBehaviour {
[Range(0.1f,20.0f)]
public float heightScale = 5.0f;
[Range(0.1f,40.0f)]
public float detailScale = 5.0f;
public GameObject wavePlane;
private Mesh myMesh;
private Vector3[] vertices;
/*
//Unit Circle properties
public Transform target;
public float orbitDistance = 1.0f;
public float orbitDegreesPerSec = 90.0f;
public GameObject Ball;
*/
void Update()
{
GenerateWave();
}
void GenerateWave()
{
myMesh = wavePlane.GetComponent<MeshFilter>().mesh;
vertices = myMesh.vertices;
int counter = 0; //i
int yLevel = 0; //j
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
CalculationMethod(counter, yLevel);
counter ++;
}
yLevel ++;
}
myMesh.vertices = vertices;
myMesh.RecalculateBounds();
myMesh.RecalculateNormals();
Destroy(wavePlane.GetComponent<MeshCollider>());
MeshCollider collider = wavePlane.AddComponent<MeshCollider>();
collider.sharedMesh = null;
collider.sharedMesh = myMesh;
}
//public bool waves = false;
//public float waveSpeed = 5.0f;
void CalculationMethod(int i, int j)
{
//radius * Mathf.Sin(speed* Time.time + vertices[i].x + phase shift)
vertices[i].z = Mathf.Sin(Time.time + vertices[i].x);
vertices[i].z -=j;
}
}
我查看了Unity资源商店,有一些波形发生器,但它们都不会以不同的幅度和频率工作,因此得到的曲线是一条连续曲线,其中先前的幅度和频率仍显示在编辑器中。
答案 0 :(得分:0)
我不太了解您将如何使用生成器,但您只需计算图表的下一个值或更新时的任何值。
void Generate(double previousPositionOnCircleInRadian, double speed,
double amplitude, out double sinValueMultiliedByAmplitude,
out double nextPositionOnCircleInRadian)
{
nextPositionOnCircleInRadian = previousPositionOnCircleInRadian + Time.deltaTime*speed;
sinValueMultiliedByAmplitude = amplitude * Math.Sin(nextPositionOnCircleInRadian);
}
我想你只需要计算你的圆圈和sinwave上的位置。