- Home /
Creating an infinite trail that can be collided with
I'm creating a 2D platformer where the player controls multiple characters that each leave a trail behind them. The trails last until the player completes the level.
If one of the characters crosses the trail of another character, I need to perform an action on the character that crossed the trail. The built-in TrailRenderer is perfect for creating the trail, but does not provide me with a way to detect collisions with it.
I found a script that is supposed to provide a solution to this (TrailRendererWith2DCollider) but I'm having serious issues with it. It creates a PolygonCollider2D around the trail and updates the path of the collider every frame. The issue is that this script completely crashes my engine if I run the game in it, and it crashes the game if I run it standalone. I've traced the issue back to the collider.SetPath(0, colliderPath);
call. For some reason, the SetPath call eventually crashes everything with no reason why. Debugging gets me nothing and there's no errors. Also, after the trail gets long enough, the performance starts to suffer severely. If I disable the SetPath call, nothing crashes and I have no performance issues.
Is there a way to use the built-in TrailRenderer and detect objects that pass over the path of the trail? Others have mentioned RayCasts, but I'm not super familiar with them and I think they require points of the path in order to cast the ray to, but as far I know you're not able to get the points of the trail using the build-in TrailRenderer.
Any and all help would be greatly appreciated! Thank you to everyone who takes the time to read this.
Answer by Raresh · Aug 18, 2015 at 10:58 AM
You could try spawning colliders at set intervals from the object emitting the trail while it's moving, then check if it hits one of those.
This sounds like a nice idea, but - if I'm understanding this correctly - would that leave gaps in the trail in the areas that don't spawn a collider?
Yes, but if the gap is smaller than the character, that would be enough for what you need right? I mean it doesn't have to fill the whole trail to detect with a pretty big accuracy if something entered the general area of it.
Answer by tanoshimi · Aug 18, 2015 at 11:00 AM
Raycasts won't help, since they also require a collider to detect the object hit by a ray. What do you get in the Editor log after the crash?
I'd guess that the set of vertices created by that script don't define a valid polygon - seeing as you only want to detect when another player crosses the trail, have you tried using an EdgeCollider2D
instead?
Ah, that makes sense. The odd thing is that I don't see anything in the Editor log after the crash. But that could be because it also it's really a crash, it's more of a freeze. When it happens, the entire editor locks up and is unusable, but doesn't actually "crash." I have to open task manager and force close it.
After you mentioned EdgeCollider2D
, I altered the script to use it ins$$anonymous$$d of the PolygonCollider2D
, and I believe that actually fixed the freezes. In my testing that would freeze the with the PolygonCollider, it doesn't freeze with the EdgeCollider. Thanks so much for that recommendation! However, the performance is still an issue when one character moves a lot - and the performance ONLY suffers when controlling the character that has a large trail behind it. When I swap to a different character with a smaller trail, the performance picks up again. I may try putting the script on a standalone GameObject ins$$anonymous$$d of the character and simply rendering the trail to the character.
Your answer
Follow this Question
Related Questions
How to get a glow/lightning trail effect 1 Answer
trails and particles 2 Answers
Brighter Trail Renderer 2 Answers
How to get Zerg Tentacle-like effect (see example in post)? 1 Answer
How do i make trail material separated? 0 Answers