positions | 要添加到轨迹的坐标。 |
将一系列坐标添加到轨迹。
TrailRenderer 中的所有点都会在它们诞生时存储一个时间戳。这与 TrailRenderer.time 属性一起用于确定它们何时将被移除。为了让轨迹平滑地消失,每个坐标都必须具有唯一的递增时间戳。当从脚本提供坐标并且当前时间对于多个点相同时,坐标时间戳会调整为在轨迹中最新现有点的時間戳和当前时间之间平滑插值。
using UnityEngine; using System.Collections; using System.Collections.Generic;
[RequireComponent(typeof(TrailRenderer))] public class ExampleClass : MonoBehaviour { public int numExtraPositions = 0; public float speed = 20.0f; public float radius = 4.0f;
private TrailRenderer tr;
void Start() { tr = GetComponent<TrailRenderer>(); tr.material = new Material(Shader.Find("Sprites/Default")); tr.time = 0.2f; tr.widthMultiplier = 0.3f; }
void Update() { float time = Time.time; tr.transform.position = CalculatePosition(time);
if (numExtraPositions > 0) { float prevTime = time - Time.deltaTime; List<Vector3> extraPositions = new List<Vector3>(numExtraPositions);
for (int i = 0; i < numExtraPositions; i++) { float percentage = (float)(i + 1) / (numExtraPositions + 1); float blendedTime = Mathf.LerpUnclamped(prevTime, time, percentage); extraPositions.Add(CalculatePosition(blendedTime)); }
tr.AddPositions(extraPositions.ToArray()); } }
void OnGUI() { GUI.Label(new Rect(25, 20, 200, 30), "Extra Positions"); numExtraPositions = (int)GUI.HorizontalSlider(new Rect(165, 25, 200, 30), (float)numExtraPositions, 0, 5); }
private Vector3 CalculatePosition(float time) { return new Vector3(Mathf.Sin(time * speed) * radius, Mathf.Cos(time * speed) * radius, 0.0f); } }