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 KarenM · Feb 22, 2013 at 12:16 PM · javascriptlinerenderercircle

Linerenderer circle not drawing as expected

I'm trying to draw out circular orbits for a solar system. I found some code and tinkered with it (most of the tinkering was converting it to Javascript), but I'm getting a weird result where (what I think is) the last part of the orbit goes back to origin. I understand the math superficially, but would appreciate some help figuring this out.

Code:

 public var radius : float = 1.0;
 
 private var theta_scale : float = 0.1;                        //Set lower to add more points
 private var size : float = (3.0 *  Mathf.PI) / theta_scale;    //Total number of points in circle.
 
 function Start()
 {
     var lineRenderer : LineRenderer = gameObject.AddComponent("LineRenderer");
     var c1 : Color = Color(0.5, 0.5, 0.5, 1);
     
     lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(c1, c1);
     lineRenderer.SetWidth(0.05, 0.05);
     lineRenderer.SetVertexCount(size);
 
     var i : int = 0;
     
     for(var theta : float = 0; theta < (2.0 *  Mathf.PI); theta += 0.1)
     {
         var x : float = radius * Mathf.Cos(theta);
         var y : float = radius * Mathf.Sin(theta);
         var pos : Vector3 = new Vector3(x, y, 0);
         
         lineRenderer.SetPosition(i, pos);
         i += 1;
     }
 }
 



Picture: alt text

Thanks in advance for any guidance.

linerendererweirdness.jpg (31.1 kB)
Comment
Add comment
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
1
Best Answer

Answer by Bunny83 · Feb 22, 2013 at 12:58 PM

You have "3.0 Mathf.PI" instead of "2.0 Mathf.PI". Also is there any reason why you calculate the vertex count from a theta_scale and not the other way round?

Usually it makes much more sense like this:

 public var radius : float = 1.0;
 
 public var vertexCount = 20;
 
 function Start()
 {
     var lineRenderer : LineRenderer = gameObject.AddComponent("LineRenderer");
     var c1 : Color = Color(0.5, 0.5, 0.5, 1);
     lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(c1, c1);
     lineRenderer.SetWidth(0.05, 0.05);
     lineRenderer.SetVertexCount(vertexCount+1);
 
     for(var i = 0; i < vertexCount+1; i++)
     {
         var angle : float = i;
         i = (i / vertexCount) * Mathf.PI * 2;
         var pos : Vector3 = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * radius;
         lineRenderer.SetPosition(i, pos);
     }
 }

I used vertexCount+1 because you need to duplicate the last point since the first and the last point should be the same.

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 KarenM · Feb 22, 2013 at 11:44 PM 0
Share

Thanks Bunny, this helped.

avatar image Hpridham · Dec 30, 2013 at 06:24 PM 0
Share

I'm using this code. It works great in scene view but my line doesn't show up with I output a build. What am I missing?

avatar image
1

Answer by KarenM · Feb 23, 2013 at 12:57 AM

I worked with a programmer friend of mine in meatspace and he suggests something very similar to this. I am happy to have two good answers. Thanks so much. Unity devs are great!

For posterity, my meatspace programmer friend suggests: public var radius : float = 1.0; private var numSegments:int = 128;

 function Start()
 {
     var lineRenderer : LineRenderer = gameObject.AddComponent("LineRenderer");
     var c1 : Color = Color(0.5, 0.5, 0.5, 1);
     
     lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(c1, c1);
     lineRenderer.SetWidth(0.05, 0.05);
     lineRenderer.SetVertexCount(numSegments + 1);
     
     var deltaTheta:float = (2.0 *  Mathf.PI) / numSegments;
     var theta:float = 0;
     
     for (var i:int = 0; i < numSegments + 1; i++)
     {
         var x : float = radius * Mathf.Cos(theta);
         var y : float = radius * Mathf.Sin(theta);
         var pos : Vector3 = new Vector3(x, y, 0);
         
         lineRenderer.SetPosition(i, pos);
         
         theta += deltaTheta;
     }
 }
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 Hpridham · Dec 30, 2013 at 06:17 PM 0
Share

I'm using this code. It works great in scene view but my line doesn't show up with I output a build. What am I missing?

avatar image
1

Answer by veinstix · Mar 19, 2016 at 02:50 PM

KarenM's revision in c#, in my case I wasn't able to see the circle till I set useWorldSpace to false. This is a late answer I know, but who knows it may help someone later.

 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 public class CircleRenderer : MonoBehaviour {
 
     [Range(0.1f, 100f)]
     public float radius = 1.0f;
 
     [Range(3, 256)]
     public int numSegments = 128;
 
     void Start ( ) {
         DoRenderer();
     }
 
     public void DoRenderer ( ) {
         LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer>();
         Color c1 = new Color(0.5f, 0.5f, 0.5f, 1);
         lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
         lineRenderer.SetColors(c1, c1);
         lineRenderer.SetWidth(0.05f, 0.05f);
         lineRenderer.SetVertexCount(numSegments + 1);
         lineRenderer.useWorldSpace = false;
 
         float deltaTheta = (float) (2.0 * Mathf.PI) / numSegments;
         float theta = 0f;
 
         for (int i = 0 ; i < numSegments + 1 ; i++) {
             float x = radius * Mathf.Cos(theta);
             float y = radius * Mathf.Sin(theta);
             Vector3 pos = new Vector3(x, y, 0);
             lineRenderer.SetPosition(i, pos);
             theta += deltaTheta;
         }
     }
 
 }
 
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

12 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

Related Questions

Having trouble using a line renderer to show a projectile's predicted trajectory. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

How to make a wavy circle? 0 Answers

How to find the radius area of linerencderer drawn circle and move objects inside ? 1 Answer

LineRenderer not drawing on game screen 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