Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by calanlucas · Dec 09, 2013 at 09:01 PM · colliderthroughtrail renderertron

How to add a collider to a trail renderer.

I have to objects that move independently and they each have a trail behind them. I want it so that they cannot go through each others trail. But each object can go through its own trail. Eventually I would like to make it so that when one object collides with the other objects trail, the game ends. But I would really just like to know how to add a collider to the trail. Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Dec 09, 2013 at 09:03 PM 0
Share

There is a solution here using linecast:

http://answers.unity3d.com/questions/418759/how-might-i-go-about-making-a-trail-render-that-de.html

And one here using colliders:

http://answers.unity3d.com/questions/285040/draw-a-line-in-game.html

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by noooodley · Jun 19, 2014 at 02:31 AM

This is a bit late, but I wrote a script that creates trail renderer with a collider for a game that I worked on. You can find it on the Unity wiki:

http://wiki.unity3d.com/index.php/TrailRendererWith2DCollider

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HariprasadA · Dec 18, 2017 at 10:35 AM 0
Share

I added this script to my player which has a rigidbody with gravity scale=0 (flying object). I want the player to collide with his trail. So I don't want a trigger collider. When I uncheck istrigger, the player keeps shaking and rotating around.... Please help.

avatar image
1

Answer by John_Sherer · Jun 13, 2014 at 08:09 PM

Sounds like tron :). I've made a pretty simple version of it, so here's the advice I have to offer:

1: Adding a collider to a trail renderer is pretty difficult stuff. I was able to get by by simply creating invisible boxes with colliders behind the moving object. Later, to improve performance, you can make it so the colliders are long and far in between, and make them shorter and more often when banking a turn.

2: Layers, Layers Layers!! You would want to put your moving object and it's trail on the same layer (next to tag). Then, in any script, you can adjust which layers will collide with each other. For example: Physics.IgnoreLayerCollision (0, 0); will make the default layer unable to collide with itself.

  1. Syncopate your trail renderer with the actual trail (make them have the same length). This may sound easy, but it was the hardest part for me. What I ended up doing was attach a self destruction script to my trail collider prefab. It went something like this: public float lifespan; private float timer; //Create this later: public AnotherScript master; void Update () { //Set the lifespan of this cube to the masterscript length value lifespan = master.length; //Keep the timer ticking! timer += Time.deltaTime; //If the timer (which keeps track of how many seconds we've been alive) is bigger than our allotted lifespan, destroy ourselves if(timer>lifespan){ GameObject.Destroy (gameObject); } }

Then, you would attach the masterscript to the trail renderer and make it also manage the length of the trail with`GetComponent().time=length` Let me know if you have any questions!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Justanotherpsychopath · Apr 19, 2021 at 09:44 AM

Hi, I come from the future. We are not doing so good (long story), but I can at least make life easier for people that are searching for ways to implement this behaviour (as I found this page googling for solutions).

Trail Renderer has a method called TrailRenderer.BakeMesh that lets you take a "snapshot" of the mesh it created and store it in another mesh (you provide as a parameter).

Now, I was working on a mobile 2D game that lets the player swipe the screen, drawing a short line, that can then interact with the game world, but I learnt that mesh colliders (which sounds like the most obvious next step) can't interact with 2d rigidbody/colliders, so I had to create a PolygonCollider2D, change its "points" (an array of Vector2Ds that contains the polygon vertices) to match the vertices of the mesh (after a little conversion from an array of Vector2s to Vector3s), everything attached to a Game Object I create when dereferenciated and dereferenciate when the swipe ends (so to have only 1 polygon per swipe but make it so there can be more then 1 swipe at any given type).

This Game Object also needs a Rigidbody2D, obviously, and you can attach to it a MeshFilter+MeshRenderer so you can clearly see it (even after the trail renderer has disappeared, if you want the effect of a sweeping color and another one below it).

The last thing: remember to check that the Trail Renderer doesn't have "Min Vertex Distance" too small and too many" End Cap Vertices" or you could get weird collision errors, or none at all.

Here is the code (I call the DrawSwipe every frame there are points in the swipe, which is a stack):

     public void DrawSwipe(TrailRenderer trail)
     {
         if (trail!= null)
         {
             Mesh mesh = new Mesh();
             trail.BakeMesh(mesh, Camera.main, true);
             SwipeObject.GetComponent<MeshFilter>().mesh = mesh;
 
             PolygonCollider2D collider = SwipeObject.GetComponent<PolygonCollider2D>();
             
             List<Vector2> verts = new List<Vector2>();
             foreach (Vector2 vertex in mesh.vertices)
             {
                 verts.Add(vertex);
             }
 
             collider.points = verts.ToArray();
         }
     }

This is the GameObject that actually collides with stuff (it's a property):

     private GameObject swipeObject;
     private GameObject SwipeObject
     {
         set => swipeObject = value;
 
         get
         {
             if (swipeObject== null)
             {
                 swipeObject = new GameObject("Swipe", typeof(PolygonCollider2D), typeof(Rigidbody2D), typeof(MeshFilter), typeof(MeshRenderer));
                 swipeObject.GetComponent<Rigidbody2D>().isKinematic = true;
                 swipeObject.GetComponent<MeshRenderer>().material = material;
             }
             return swipeObject;
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trail renderer physics (Tron like game) 2 Answers

sphere gets into cube 1 Answer

Adding a spring or joint makes object fall through object 0 Answers

Some objects falls through Plane with mesh collider(grenade) 2 Answers

ball sometimes goes through the bat when hit... collider problem... How to do a Raycast? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges