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
2
Question by tehb0x · Mar 09, 2015 at 02:09 PM · unity 52dlinerendererline

LineRenderer just showing awful results

Hi,

I'm trying to draw the outline of 2d colliders in gameplay. As far as I know the only way to draw a line on Unity Free is to use linerenderer or to buy some linedrawer from the asset store. LineRenderer gives awkward results because its meant to be 3D, here is what it looks like when I try to draw the outline of a BoxCollider2D:

alt text

Is there any way how I can achieve this in unity free? I also should be able to draw the outlines of a circle and polygon, but it's okay when I can implement these 2 later, I don't need them right now.

linerenderer.png (773 B)
Comment
Add comment · Show 3
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 maccabbe · Mar 09, 2015 at 04:43 PM 0
Share

Seems to work fine for me

alt text

It seems you are setting the start width and end with to different values. If you are using inspector check these value. If you are using script then make sure you use lineRenderer.SetWidth(c, c) so both start and end width are the same.

temp.png (321 B)
avatar image tehb0x · Mar 09, 2015 at 05:05 PM 0
Share

Hm I have the same width, that's the entire code maybe something else is wrong:

(Initialising)

_lineRenderer = UnityObject.AddComponent(); _lineRenderer.SetColors(Color.red, Color.red); _lineRenderer.SetWidth(0.05f, 0.05f); _lineRenderer.SetVertexCount(5); _lineRenderer.useWorldSpace = false;

(When xSize or ySize got changed)

             _lineRenderer.SetPosition(0, new Vector3(0, 0, z));
             _lineRenderer.SetPosition(1, new Vector3(xSize, 0, z));
             _lineRenderer.SetPosition(2, new Vector3(xSize, -ySize, z));
             _lineRenderer.SetPosition(3, new Vector3(0, -ySize, z));
             _lineRenderer.SetPosition(4, new Vector3(0, 0, z));
avatar image fafase · Mar 09, 2015 at 05:15 PM 0
Share

Your xside seems to be flipping the line.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by maccabbe · Mar 09, 2015 at 06:34 PM

Line renderer was simply not meant to draw multiple lines if any angle was less than or equal to 90. Line renderer prioritizes keeping the width constant but it can't do a good job if the angle between two lines is less than or equal to 90 because it considers the width of the vertex. However you probably want the vertices like they would in a frame so you have a few options.

1) Use multiple line renderers. I did this in my comment.

2) Make your own mesh and use mesh renderer. For instance, you could use something like the following.

 public class NewBehaviourScript : MonoBehaviour {
     Mesh mesh;
     void Start() {
         MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
         renderer.material=new Material(Shader.Find("Diffuse"));
         renderer.material.color=Color.red;
 
         MeshFilter filter=gameObject.AddComponent<MeshFilter>();
         mesh=new Mesh();
         mesh.vertices=new Vector3[8];
         mesh.triangles=new int[]{
             0, 1, 4,
             5, 4, 1,            
             1, 3, 5,
             3, 7, 5,            
             3, 2, 6,
             6, 7, 3,            
             2, 0, 4,
             6, 2, 4,   
         };
         mesh.uv=new Vector2[mesh.vertices.Length];
         mesh.RecalculateNormals();
         mesh.Optimize();
         filter.mesh=mesh;
     }
 
     public float z=0;
     public float xSize=1;
     public float ySize=1;
     float width=1f;
 
     void Update() {
         mesh.vertices=new Vector3[]{
             new Vector3(-width, -width, z),
             new Vector3(-width, width+ySize, z),
             new Vector3(width+xSize, -width, z),
             new Vector3(width+xSize, width+ySize, z),
 
             new Vector3(width, width, z),
             new Vector3(width, -width+ySize, z),
             new Vector3(-width+xSize, width, z),
             new Vector3(-width+xSize, -width+ySize, z),
         };
         mesh.RecalculateNormals();
     }
 }
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
4

Answer by endeef · Mar 05, 2019 at 04:13 PM

I had the same problem. Using widthMultiplier instead of start-/endWidth fixed it for me.

alt text

 LineRend.startWidth = LineWidth;
 LineRend.endWidth = LineWidth;

alt text

 LineRend.widthMultiplier = LineWidth;


widthmultiplier.png (23.5 kB)
startendwidth.png (20.4 kB)
Comment
Add comment · Show 2 · 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 ian1971 · Apr 11, 2019 at 12:54 PM 0
Share

width$$anonymous$$ultiplier totally fixed it for me. I left startWidth and endWidth to 1 and set width multiplier as indicated.

avatar image falkenbrew · Jul 10, 2020 at 01:21 PM 0
Share

fixed my problem in 2019.3

avatar image
0

Answer by nathanthesnooper · Oct 23, 2016 at 11:37 PM

@maccabbe :

1) Use multiple line renderers. I did this in my comment.

This worked for me, but anyone still having this problem should update to Unity 5.5.0xb5 or later, Unity seemed to have fixed this bug.

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

25 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

Related Questions

What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer

How can I create a glowing, smooth line of any shape? 2 Answers

Stop LineRenderer if it collides with something 1 Answer

Update LineRenderer every frame (Delete Old Lines) 1 Answer

LineRenderer: how to draw line intersections? 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