简体   繁体   English

删除轨迹渲染器中的点

[英]Delete point in trail Renderer

I was wondering if there would be a way to delete the last couple of positions/indexes of a trail renderer.我想知道是否有办法删除尾迹渲染器的最后几个位置/索引。 I am trying to stop the trail render emission after a bullet collision, but the trail renderer always spawns a couple of indexes too much, and I was wondering if there was a way to delete those indexes.我试图在子弹碰撞后停止轨迹渲染发射,但轨迹渲染器总是产生过多的索引,我想知道是否有办法删除这些索引。

I have tried setting the positions of newer indexes to equal older indexes, and that worked to some extent but not to the extent that I wanted.我尝试将较新索引的位置设置为与旧索引相等,这在一定程度上起作用,但没有达到我想要的程度。

tR = GetComponent<TrailRenderer>();
int positions = tR.positionCount;

for (int i = 0; i < vertsToDelete; i++) {
    if (positions - 1 - i - (int)vertsToDelete > 0) {
        tR.SetPosition(positions - 1 - i, tR.GetPosition(positions - 1 - i - (int)vertsToDelete));
    }
}

This code works mostly except for certain instances when the positions screw up.除了位置搞砸的某些情况外,此代码主要工作。 Thats why i think being able to delete an index would make the process much easier.这就是为什么我认为能够删除索引会使这个过程更容易。

For anyone still searching for this.对于仍在寻找此内容的任何人。 I had an issue just like that, my Trail Renderer was drawing some artifact when I used the Floating Origin on it (even when I turned emission off, it was like it had a delay on turning off).我遇到了这样的问题,当我使用Floating Origin时,我的Trail Renderer正在绘制一些工件(即使我关闭了发射,它就像关闭时有延迟一样)。

After 2 weeks trying many things and searching I solved this in the most ridiculous way:经过两周的尝试和搜索,我以最荒谬的方式解决了这个问题:

    //I have this at the beginning os my class:
    //public TrailRenderer trailRenderer;

    //and this declared at start:
    //trailRenderer = trailRenderer.GetComponent<TrailRenderer>();

    int trailSize = trailRenderer.positionCount;
    float distance = Vector3.Distance(trailRenderer.GetPosition(trailSize - 1), (trailRenderer.GetPosition(trailSize - 2)));
    Debug.Log("Distance: " + distance);

    if (distance > 90f)
    {
        trailRenderer.SetPosition(trailSize - 1, trailRenderer.transform.position);
    }

This verify the wrong Draw (in my case is just the last point) and get it back to my trail renderer transform, preventing any artifacts my floating origin may cause if it causes any .这会验证错误的 Draw(在我的情况下只是最后一点)并将其返回到我的跟踪渲染器转换中,从而防止我的浮动原点在导致任何. That's way better than use a for to verify every position since I only have an issue with the last one.这比使用 for 验证每个位置要好得多,因为我只有最后一个有问题。 Just adapt that to your needs :)只需根据您的需求进行调整即可:)

It is more efficient to use one single GetPositions call, manipulate the array and write it back completely using one single AddPositions call. 使用单个GetPositions调用,操作数组并使用单个AddPositions调用将其完全写回更为有效。

In order to alternate the array you could go through a list which allows dynamically changing the amount of elements more easily. 为了替换数组,您可以查看一个列表,该列表允许更轻松地动态更改元素数量。

Start by converting the array to a list using Linq IEnumerable.ToList() 首先使用Linq IEnumerable.ToList()将数组转换为列表

var positions = new Vector3[tr.positionCount];
tr.GetPositions(positions);
var positionsList = positions.ToList();

then remove an element by index using List<T>.RemoveAt(int index) 然后使用List<T>.RemoveAt(int index)按索引删除元素

positionsList.RemoveAt(indexToRemove);

or remove multiple sequential elements using List<T>.RemoveRange(int startIndex, int amount) 或使用List<T>.RemoveRange(int startIndex, int amount)删除多个顺序元素

positionsList.RemoveRange(startIndex, amountToRemove);

And finally convert it back to the required arrays using List<T>.ToArray() 最后使用List<T>.ToArray()将其转换回所需的数组

tR.Clear();
tR.AddPositions(positionsList.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM