Tôi đã tạo ra một đường cong bezier bằng cách thêm đoạn mã sau vào một đối tượng trò chơi trống trong thanh tra. Điều này rút ra để hoàn thành đường cong cùng một lúc khi tôi chạy mã. Làm thế nào tôi có thể tạo hiệu ứng động trong một khoảng thời gian nhất định, nói 2 hoặc 3 giây?Làm thế nào để animate một đường cong bezier trong một thời hạn nhất định
public class BCurve : MonoBehaviour {
LineRenderer lineRenderer;
public Vector3 point0, point1, point2;
int numPoints = 50;
Vector3[] positions = new Vector3[50];
// Use this for initialization
void Start() {
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material (Shader.Find ("Sprites/Default"));
lineRenderer.startColor = lineRenderer.endColor = Color.white;
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
lineRenderer.positionCount = numPoints;
DrawQuadraticCurve();
}
void DrawQuadraticCurve() {
for (int i = 1; i < numPoints + 1; i++) {
float t = i/(float)numPoints;
positions [i - 1] = CalculateLinearBeziearPoint (t, point0, point1, point2);
}
lineRenderer.SetPositions(positions);
}
Vector3 CalculateLinearBeziearPoint (float t, Vector3 p0, Vector3 p1, Vector3 p2) {
float u = 1 - t;
float tt = t * t;
float uu = u * u;
Vector3 p = uu * p0 + 2 * u * t * p1 + tt * p2;
return p;
}
}
Từ hiểu biết của tôi về câu hỏi, chronos muốn tạo ảnh động đường cong bezier trong khoảng thời gian cụ thể. – Hilarious404
@ Hilarious404 sau đó tham số WaitForSeconds phải là duration/numPoints – mayo
Trừ khi kết thúc sẽ nhỏ hơn 'Time.deltaTime', trong trường hợp đó nhiều lần chạy qua vòng lặp trong một khung đơn sẽ là cần thiết. –