简体   繁体   中英

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).

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 . That's way better than use a for to verify every position since I only have an issue with the last one. 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.

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()

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)

positionsList.RemoveAt(indexToRemove);

or remove multiple sequential elements using List<T>.RemoveRange(int startIndex, int amount)

positionsList.RemoveRange(startIndex, amountToRemove);

And finally convert it back to the required arrays using List<T>.ToArray()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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