| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(LineRenderer))]
- public class SingleWaveLineRenderer : MonoBehaviour
- {
- public LineRenderer lineRenderer;
- public int pointCount = 100;
- public float lineLength = 6f;
- public float amplitude = 0.5f;
- public float wavelength = 2f;
- public float waveSpeed = 2f;
- public float phaseOffset = 0f;
- public Transform startPointTransform; // ✅ 控制起点位置和方向的锚点
- private float frequency;
- void Start()
- {
- frequency = 2 * Mathf.PI / wavelength;
- lineRenderer.positionCount = pointCount;
- if (lineRenderer.material != null)
- lineRenderer.material.color = Color.cyan;
- }
- void Update()
- {
- float step = lineLength / (pointCount - 1);
- float time = Time.time;
- Vector3 startPos = startPointTransform.position;
- Vector3 direction = startPointTransform.right; // ✅ 方向:起点 Transform 的右方向
- for (int j = 0; j < pointCount; j++)
- {
- float x = j * step;
- float y = amplitude * Mathf.Sin(frequency * x - waveSpeed * time + phaseOffset);
- // ✅ 构建偏移:方向 + 垂直扰动(up 方向)
- Vector3 offset = direction.normalized * x + startPointTransform.up * y;
- lineRenderer.SetPosition(j, startPos + offset);
- }
- }
- }
|