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?
var degreesPerSegment : float;
var radialScale : float;
private var curAngle : float = 0;
private var l_Renderer : LineRenderer;
function Awake()
{
l_Renderer = GetComponent(LineRenderer);
l_Renderer.SetVertexCount = 360/degreesPerSegment+1;
}
function CreatePoints()
{
var x : float;
var y : float;
var z : float = 0;
for (var i : int = 0;i < 360/degreesPerSegment + 1;i++)
{
x=Mathf.Sin(Mathf.Deg2Rad(curAngle));
y=Mathf.Cos(Mathf.Deg2Rad(curAngle));
l_Renderer.SetPosition(i,Vector3(x,y,z)*radialScale);
currentAngle += degreesPerSegment;
}
}
Function Update()
{
CreatePoints();
}
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?
Ok. Solved this one. It was a matter of the camera not being set to orthographic.
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
What error messages are you getting?
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’.
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!
var degreesPerSegment : int;
var radialScale : float;
private var curAngle : float = 0;
private var l_Renderer : LineRenderer;
function Awake(){
l_Renderer = GetComponent(LineRenderer);
l_Renderer.SetVertexCount(360/degreesPerSegment+1);
}
function CreatePoints(){
var x : float;
var y : float;
var z : float;
for (var i : int = 0; i < 360/degreesPerSegment + 1; i++){
x=Mathf.Sin(curAngle * Mathf.Deg2Rad);
y=Mathf.Cos(curAngle * Mathf.Deg2Rad);
l_Renderer.SetPosition(i,Vector3(x,y,z)*radialScale);
curAngle += degreesPerSegment;
}
}
function Update(){
CreatePoints();
}
Enjoy!
Will
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!
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:
using UnityEngine;
using System.Collections;
public class Circle : MonoBehaviour
{
public int segments;
public float radius;
LineRenderer line;
void Start ()
{
line = gameObject.GetComponent<LineRenderer>();
line.SetVertexCount (segments + 1);
line.useWorldSpace = false;
CreatePoints ();
}
void CreatePoints ()
{
float x;
float y;
float z = 0f;
float angle = 0f;
for (int i = 0; i < (segments + 1); i++)
{
x = Mathf.Sin (Mathf.Deg2Rad * angle);
y = Mathf.Cos (Mathf.Deg2Rad * angle);
line.SetPosition (i,new Vector3(x,y,z) * radius);
angle += (360f / segments);
}
}
}
2 Likes