CatlikeCoding-Unity/Assets/Script/Graph.cs

42 lines
1.1 KiB
C#

using UnityEngine;
public class Graph : MonoBehaviour
{
[SerializeField]
Transform pointPrefab = default;
[SerializeField, Range(10, 100)]
int resolution = 10;
Transform[] points;
private void Awake()
{
var step = 2.0f / resolution;
var position = Vector3.zero;
var scale = Vector3.one * step;
points = new Transform[resolution];
for (int i = 0; i < points.Length; i++)
{
Transform point = Instantiate(pointPrefab);
position.x = (i + 0.5f) * step - 1.0f;
point.localPosition = position;
point.localScale = scale;
point.SetParent(transform, false);
points[i] = point;
}
}
private void Update()
{
var time = Time.time;
for (int i = 0; i < points.Length; i++)
{
Transform point = points[i];
Vector3 position = point.localPosition;
position.y = Mathf.Sin(Mathf.PI * (position.x + time));
point.localPosition = position;
}
}
}