Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Unity’s Demo Team is excited to share their upcoming interactive demo, Book of the Dead. See more information here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[SOLVED]Help with LineRenderer Script

Discussion in 'Scripting' started by Maker16, Jul 7, 2009.

  1. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    I created a lineRenderer and attached the below script to it. It draws a circle. That works fine. However, when I roatate the lineRenderer object (it is not using world space), it rotates almost perfectly, except it has a bit of wobble to it, as if the center of rotation was not quite the center axis of the circle. The best way to describe it is to imagine rotating the circle around the y-axis, but with the circle offset along the z-axis by .5 units. It wobbles like that set-up would if you rotated it. I don't know if this is an issue with the renderer itself, or with how I'm calculating the points in the circle. Any thoughts?

    Code (csharp):
    1.  
    2. var degreesPerSegment : float;
    3. var radialScale : float;
    4. private var curAngle : float = 0;
    5. private var l_Renderer : LineRenderer;
    6.  
    7. function Awake()
    8. {
    9.    l_Renderer = GetComponent(LineRenderer);
    10.    l_Renderer.SetVertexCount = 360/degreesPerSegment+1;
    11. }
    12. function CreatePoints()
    13. {
    14.    var x : float;
    15.    var y : float;
    16.    var z : float = 0;
    17.    for (var i : int = 0;i < 360/degreesPerSegment + 1;i++)
    18.    {
    19.       x=Mathf.Sin(Mathf.Deg2Rad(curAngle));
    20.       y=Mathf.Cos(Mathf.Deg2Rad(curAngle));
    21.       l_Renderer.SetPosition(i,Vector3(x,y,z)*radialScale);
    22.       currentAngle += degreesPerSegment;
    23.    }
    24. }
    25. Function Update()
    26. {
    27.     CreatePoints();
    28. }
    29.    
    30.  
    EDIT: I should also note that it is being displayed as a UI element, so it has its own camera. Could the problem be that the camera is not set to orthographic?
     
  2. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    Ok. Solved this one. It was a matter of the camera not being set to orthographic.
     
  3. deadbug

    deadbug

    Joined:
    Sep 9, 2009
    Posts:
    165
    hey there,
    i really like the idea of this script but cant seem to get it to work. i am just new at this and learning by doing. it seems like this script just has a few typos. can anyone let me know if they know where the problems are?

    tx
    db
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    What error messages are you getting?
     
  5. deadbug

    deadbug

    Joined:
    Sep 9, 2009
    Posts:
    165
    hey there , here are the error messages i get. i put it in a script and attached it to main camera. thanks lots!

    l_Renderer = GetComponent(LineRenderer);
    has error :

    Assets/NewBehaviourScript.js(8,12): BCE0049: Expression cannot be assigned to.

    for (var i : int = 0;i < 360/degreesPerSegment + 1;i++)
    has error :

    Assets/NewBehaviourScript.js(17,25): BCE0077: It is not possible to invoke an expression of type 'float'.

    y=Mathf.Cos(Mathf.Deg2Rad(curAngle));

    has error :

    Assets/NewBehaviourScript.js(20,7): BCE0005: Unknown identifier: 'currentAngle'.
     
  6. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    589
    Hi guys

    got passed this script by a very helpful dude over at http://unity3dpulse.com/.. Saw it wasn't quite finished, so here's the fixed version if anyone wants it!

    Code (csharp):
    1.  
    2. var degreesPerSegment : int;
    3. var radialScale : float;
    4. private var curAngle : float = 0;
    5. private var l_Renderer : LineRenderer;
    6.  
    7. function Awake(){
    8.    l_Renderer = GetComponent(LineRenderer);
    9.    l_Renderer.SetVertexCount(360/degreesPerSegment+1);
    10. }
    11.  
    12. function CreatePoints(){
    13.    var x : float;
    14.    var y : float;
    15.    var z : float;
    16.    
    17.    for (var i : int = 0; i < 360/degreesPerSegment + 1; i++){
    18.    
    19.       x=Mathf.Sin(curAngle * Mathf.Deg2Rad);
    20.       y=Mathf.Cos(curAngle * Mathf.Deg2Rad);
    21.       l_Renderer.SetPosition(i,Vector3(x,y,z)*radialScale);
    22.       curAngle += degreesPerSegment;
    23.      
    24.    }
    25. }
    26.  
    27. function Update(){
    28.    CreatePoints();
    29. }
    Enjoy!

    Will
     
  7. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    589
    Edit : Better to place the call to CreatePoints in a Start or Awake rather than Update if you just want to create it, and not constantly run the function, oops!
     
  8. Leiter Jakab

    Leiter Jakab

    Joined:
    Sep 25, 2010
    Posts:
    53
    Thanks for the script. I rewrote it a bit in C#. There were some mistakes like the SetVertexCount and Deg2Rad not used correctly. The second one is misleading because it's not a function, but a variable (a conversion constant). Anyways here's my version:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Circle : MonoBehaviour
    5. {
    6.     public int segments;
    7.     public float radius;
    8.     LineRenderer line;
    9.        
    10.     void Start ()
    11.     {
    12.         line = gameObject.GetComponent<LineRenderer>();
    13.        
    14.         line.SetVertexCount (segments + 1);
    15.         line.useWorldSpace = false;
    16.         CreatePoints ();
    17.     }
    18.    
    19.    
    20.     void CreatePoints ()
    21.     {
    22.         float x;
    23.         float y;
    24.         float z = 0f;
    25.        
    26.         float angle = 0f;
    27.        
    28.         for (int i = 0; i < (segments + 1); i++)
    29.         {
    30.             x = Mathf.Sin (Mathf.Deg2Rad * angle);
    31.                     y = Mathf.Cos (Mathf.Deg2Rad * angle);
    32.                     line.SetPosition (i,new Vector3(x,y,z) * radius);
    33.                     angle += (360f / segments);
    34.         }
    35.     }
    36. }
     
    Last edited: Dec 23, 2010
    felixmann likes this.