这是在这里提出的问题的继续:获取路径渲染器的长度
null
private TrailRenderer trail;
void Start () {
trail = GetComponent<TrailRenderer>();
Length = 0;
trail.Clear();
}
void Update () {
GetTrailLength ();
Debug.Log ("Length: " + Length);
if (Length > 1.0) {
trail.material.color = Color.red;
} else {
trail.material.color = Color.white;
}
}
public void GetTrailLength () {
Length = 0f;
var points = new Vector3[trail.positionCount];
var count = trail.GetPositions (points);
// Store the first position
var start = points[0];
// Iterate through the rest of positions
for (var i = 1; i < count; i++) {
// get the current position
var end = points[i];
// Add the distance to the last position
Length += Vector3.Distance (start, end);
// update the start position for the next iteration
start = end;
}
}
如果你不想要一个精确的长度,就不要使用float。
if ((int)Length >= 1) {
trail.material.color = Color.red;
} else {
trail.material.color = Color.white;
}
看看这有没有帮助。