Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Mar 04 at 10:24 AM by JaredHD for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JaredHD · Oct 18, 2017 at 02:04 PM · c#meshlinerendererproceduralnavigation

Generate a mesh around a LineRenderer

Hello, I was wondering if there is an easy way to generate a mesh for a LineRenderer?

I followed this tutorial to make a spline and I was hoping I could attach unity's new navmesh surface component to it.

Sadly I didn't realize that the LineRenderer wasn't a mesh and so it won't work with the navigation system.

I have all the vertexes for the line. Is the only way to do it to generate mesh individually?

alt text

line.png (232.9 kB)
Comment
Add comment · Show 2
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 BlakeSchreurs · Oct 18, 2017 at 02:30 PM 0
Share

This is pretty doable. The big questions are: What shape do you want to generate around the line, What do you want to do about corners?

avatar image JaredHD BlakeSchreurs · Oct 18, 2017 at 02:39 PM 0
Share

Well, what I'm trying to do is make it flat. I trying to make a path for NPC's to walk on at runtime.

I kinda want the path to look exactly like the line drawn in the image, where it bends at the corners. so the shape is rectangular and rounder edges.

If you have any ideas I would be more than glad to hear them. I can upload the scripts also if you want them.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by BlakeSchreurs · Oct 18, 2017 at 06:39 PM

Here is some starter code. There are plenty of examples on how to dynamically create a mesh from points, so my answer here is going to focus on creating a group of points from a line. In this case, there will be a "left" side and a "right" side, and you can create a triangle strip using the two sets of points. The left side is one meter to the left of center, and the right side is one meter to the right of center, creating a path two meters wide.

alt text

This code is not a solve-all-cases piece of code. The sharper the angles of the turn, the more the weaknesses of this approach will show. That said, it's a fairly simple piece of code to understand, and you can build upon it to deal with the edge cases that you may encounter.

Also remember: You can run it in editor, and then prefab-out the generated result which you can then re-use without having to go through the process of generation.

This code finds & debug displays the left & right side of the line, but does not create the mesh.

 using UnityEngine;
 
 public class PathScript : MonoBehaviour {
 
     public LineRenderer line;
     
     // Use this for initialization
     void Start () {
         var caret = new GameObject();
         Vector3 left, right; // A position to the left of the current line
 
         // For all but the last point
         for (var i = 0; i < line.positionCount - 1; i++)
         {
             caret.transform.position = line.GetPosition(i);
             caret.transform.LookAt(line.GetPosition(i + 1));
             right = caret.transform.position + caret.transform.right;
             left = caret.transform.position - caret.transform.right;
             Debug.DrawLine(line.GetPosition(i), line.GetPosition(i) + Vector3.up, Color.white, 60f); // Just to visualize what's going on
             Debug.DrawLine(left, left + Vector3.up, Color.blue, 60f); // Just to visualize what's going on
             Debug.DrawLine(right, right + Vector3.up, Color.red, 60f); // Just to visualize what's going on
         }
 
         // Last point looks backwards and reverses
         caret.transform.position = line.GetPosition(line.positionCount - 1);
         caret.transform.LookAt(line.GetPosition(line.positionCount - 2));
         right = caret.transform.position - caret.transform.right;
         left = caret.transform.position + caret.transform.right;
         Debug.DrawLine(caret.transform.position, caret.transform.position + Vector3.up, Color.white, 60f); // Just to visualize what's going on
         Debug.DrawLine(left, left + Vector3.up, Color.blue, 60f); // Just to visualize what's going on
         Debug.DrawLine(right, right + Vector3.up, Color.red, 60f); // Just to visualize what's going on
     }
 }
 



path.jpg (16.4 kB)
Comment
Add comment · Show 6 · 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 JaredHD · Oct 19, 2017 at 04:21 AM 0
Share

Thank you, I think this is leading me in the right direction. I'm gonna try to implement a mesh builder for it. If I can do that I'll accept your answer.

avatar image JaredHD · Oct 19, 2017 at 06:58 AM 0
Share

Thank you :)

Here is the extra code that made it work:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class PathScript : $$anonymous$$onoBehaviour
 {
     public LineRenderer line;
     private List<Vector3> points = new List<Vector3>();
 
     // Use this for initialization
     public void start(LineRenderer lines)
     {
         points.Clear();
         line = lines;
         GameObject caret = null;
         caret = new GameObject("Lines");
 
         Vector3 left, right; // A position to the left of the current line
 
         // For all but the last point
         for (var i = 0; i < line.positionCount - 1; i++)
         {
             caret.transform.position = line.GetPosition(i);
             caret.transform.LookAt(line.GetPosition(i + 1));
             right = caret.transform.position + transform.right * line.startWidth / 2;
             left = caret.transform.position - transform.right * line.startWidth / 2;
             points.Add(left);
             points.Add(right);
         }
 
         // Last point looks backwards and reverses
         caret.transform.position = line.GetPosition(line.positionCount - 1);
         caret.transform.LookAt(line.GetPosition(line.positionCount - 2));
         right = caret.transform.position + transform.right * line.startWidth / 2;
         left = caret.transform.position - transform.right * line.startWidth / 2;
         points.Add(left);
         points.Add(right);
         Destroy(caret);
         Draw$$anonymous$$esh();
     }
 
     private void Draw$$anonymous$$esh()
     {
         Vector3[] verticies = new Vector3[points.Count];
 
         for (int i = 0; i < verticies.Length; i++)
         {
             verticies[i] = points[i];
         }
 
         int[] triangles = new int[((points.Count / 2) - 1) * 6];
 
         //Works on linear patterns tn = bn+c
         int position = 6;
         for (int i = 0; i < (triangles.Length / 6); i++)
         {
             triangles[i * position] = 2 * i;
             triangles[i * position + 3] = 2 * i;
 
             triangles[i * position + 1] = 2 * i + 3;
             triangles[i * position + 4] = (2 * i + 3) - 1;
 
             triangles[i * position + 2] = 2 * i + 1;
             triangles[i * position + 5] = (2 * i + 1) + 2;
         }
 
 
         $$anonymous$$esh mesh = GetComponent<$$anonymous$$eshFilter>().mesh;
         mesh.Clear();
         mesh.vertices = verticies;
         mesh.triangles = triangles;
         mesh.RecalculateNormals();
     }
 }
avatar image vyshetty JaredHD · May 29, 2020 at 07:35 PM 0
Share

How to increase the width of the mesh created.

avatar image sethi-sahil27 vyshetty · Jun 08, 2020 at 11:25 PM 0
Share

This is good, but works only in some cases. I am using this to convert my points from a line renderer to a mesh, so a line is drawn horizontally does not get any kind of mesh. The rest of the cases that work fine have counter clockwise direction and are not visible in the direction of the camera. Is there a better algorithm than this to find out the triangles?

Show more comments
Show more comments
avatar image
0

Answer by tonytopper · Mar 03 at 10:23 PM

I'd suggest giving the LineRenderer component's built-in BakeMesh method a try.

https://docs.unity3d.com/ScriptReference/LineRenderer.BakeMesh.html

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

Follow this Question

Answers Answers and Comments

419 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Procedural generated mesh problem 1 Answer

Filling area under linerenderer 2 Answers

Procedural array of meshes problem / solved 1 Answer

LineRenderer BakeMesh is turning/rotating the line? 1 Answer

Do I need to split my mesh up to properly UV texture it? 1 Answer


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