positions | 要添加到轨迹的多个位置。 |
向轨迹添加位置数组。
TrailRenderer 中的所有点都会在出生时存储时间戳。此时间戳与 TrailRenderer.time 属性一起用于确定何时移除它们。为了使轨迹顺利消失,每个位置必须具有递增的唯一时间戳。当从脚本提供位置并且当前时间对于多个点相同时,需要调整位置时间戳以在路径中的最新现有点的时间戳和当前时间之间进行平滑插值。
using UnityEngine; using System.Collections; using System.Collections.Generic;
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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.