• 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
Question by Matt-Face · Jul 26, 2014 at 01:22 PM · linerendererline

LineRenderer creating triangles instead of lines

Hi all, I am having 2 problems drawing a line between four points.

  1. my line is not drawing correctly (some parts appear as triangles)...

alt text

  1. when i flip the parent object of the line renderer by multiplying the x value in the transform's localScale by -1, i get an odd result... the one part of the line that is working as i expected (the lower section) appears empty and i just get more triangles in the other sections, instead of getting the mirror image of the original line...

alt text

my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     public Transform point1;
     public Transform point2;
     public Transform point3;
     public Transform point4;
     private GameObject flashObject;
     private LineRenderer flashLineRenderer;
     public Color flashColour = Color.white;
     
     void Start () 
     {
         flashObject = new GameObject("FlashObject");
         //ADD A LINE RENDERER
         flashLineRenderer = flashObject.AddComponent<LineRenderer>();
         Material flashLineMaterial = new Material(Shader.Find("Unlit/Transparent"));
         flashLineRenderer.material = flashLineMaterial;
         flashLineRenderer.SetVertexCount(4);
         flashLineRenderer.SetWidth(1f, 1f);
         flashLineRenderer.SetPosition(0, point1.position);
         flashLineRenderer.SetPosition(1, point2.position);
         flashLineRenderer.SetPosition(2, point3.position);
         flashLineRenderer.SetPosition(3, point4.position);
         flashLineRenderer.useWorldSpace = false;
         flashLineRenderer.SetColors(flashColour, flashColour);
         flashObject.transform.parent = this.gameObject.transform;
         
     }
     
 }
 

it would be great to get a bit of insight into what is causing these problems.

Thanks in advance!

triangles.png (25.9 kB)
screen shot 2014-07-26 at 8.39.25 pm.png (13.9 kB)
Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by jbarbeau · Aug 25, 2014 at 10:09 PM

alt text

I'm having the same problem with LineRenderer. The first segment renders a triangle, unless I look at the other side. From that perspective it's a rectangle. The last segment is always a rectangle.

I've played with linewidth etc, Here's my code:

     lineRenderer.SetVertexCount (3);
     lineRenderer.SetPosition (0, new Vector3(0.0f,0.0f,0.0f)); 
     lineRenderer.SetPosition (1, new Vector3(5.0f,5.0f,5.0f)); 
     lineRenderer.SetPosition (2, new Vector3(7.0f,2.0f,6.0f)); 
     lineRenderer.SetWidth(0.1f,0.1f); 

Does anyone have an answer? Are there 2 sides to the line? It's a billboard that always faces you.

One side renders a triangle, the other side renders a rectangle look at pics the underside pic.

alt text


drivingrange_linerenderer.jpg (118.7 kB)
drivingrange_linerenderer3.jpg (83.0 kB)
Comment

People who like this

0 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 jbarbeau · Aug 25, 2014 at 10:20 PM 0
Share

alt text

I think I figured it out. Theres a singularity that occurs in the billboard logic for LineRenderer. Look at LineRenderer4 from a high angle you see it.

Now linerenderer5 looks OK, from front or back actually... If you look at a segment from an angle nearly parallel to the linesegment, you can get a triangle.

For me, it shouldn't matter, I will be rendering golf ball trajectories with lots of small segments.

alt text

drivingrange_linerenderer4.jpg (78.8 kB)
drivingrange_linerenderer5.jpg (113.6 kB)
avatar image

Answer by MaLlorente · Oct 17, 2016 at 09:17 AM

Hi @Grin and @jbarbeau,

As you can read here. The problem is that a line render is a mesh and Unity optimize the mesh by reusing some vertex. The result is that ugly triangle shape.

You can solve this by

  1. Creating more points to smooth the edges. Its not an elegant solution but its simple and works, at least for me. I wrote a function that do that:

Vector3[] Generate_Points(Vector3[] keyPoints, int segments=100){ Vector3[] Points = new Vector3[(keyPoints.Length - 1) segments + keyPoints.Length]; for(int i = 1; i < keyPoints.Length;i++){ Points [(i - 1) segments + i - 1] = new Vector3(keyPoints [i-1].x,keyPoints [i-1].y,0); for (int j = 1;j<=segments;j++){ float x = keyPoints [i - 1].x; float y = keyPoints [i - 1].y; float z = 0;//keyPoints [i - 1].z; float dx = (keyPoints [i].x - keyPoints [i - 1].x)/segments; float dy = (keyPoints [i].y - keyPoints [i - 1].y)/segments; Points [(i - 1) segments + j + i - 1] = new Vector3 (x+dx*j,y+dy*j,z); } } Points [(keyPoints.Length - 1) segments + keyPoints.Length - 1] = new Vector3(keyPoints [keyPoints.Length-1].x,keyPoints [keyPoints.Length-1].y,0); return Points; }

  1. (Solution copied form here)Coding your own line renderer, and not make it so economical. You can do this by generating a dynamic mesh. The mesh will consist of a series of thin quads. For each line segment, you can compute the four corners of the quad by calculating the normal of the line and a specified line width:

Vector3 normal = Vector3.Cross(start, end); Vector3 side = Vector3.Cross(normal, end-start); side.Normalize(); Vector3 a = start + side (lineWidth / 2); Vector3 b = start + side (lineWidth / -2); Vector3 c = end + side (lineWidth / 2); Vector3 d = end + side (lineWidth / -2);

Comment

People who like this

0 Show 0 · 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

Answer by Zodiarc · Oct 17, 2016 at 09:20 AM

If you don't need the line to be blocky, you can use the Chaikin path smoothing algorithm http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html It will produce a nicely smooth curve.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bezier curver using LineRenderer 0 Answers

How to draw a line between objects and get distance 1 Answer

LineRenderer lagging behind 1 Answer

Is there a way to slowly resize a line renderer? 1 Answer

Line renderer is not working for Perspective camera. 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