I am dabbling in unity again :3
My issue is that the white circle C# script attached to a gameobject with lineRenderer, draws in the center of camera.
How should I change my code so I can input cordinates and draw the circle anywhere I want?
Let’s say I want to draw one circle at screen (0,0) and (screen.Width,screen.Height)
How would I program this?
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof(LineRenderer))]
public class Ellipse : MonoBehaviour
{
public Vector2 radius = new Vector2(1f, 1f);
public float width = 1f;
public float rotationAngle = 90;
public int resolution = 500;
private Vector3[] positions;
private LineRenderer self_lineRenderer;
Color c1 = Color.white;
Color c2 = new Color(1, 1, 1, 1);
void OnValidate()
{
UpdateEllipse();
}
public void UpdateEllipse()
{
if ( self_lineRenderer == null)
self_lineRenderer = GetComponent<LineRenderer>();
self_lineRenderer.positionCount = resolution+3;
self_lineRenderer.startWidth = width;
self_lineRenderer.endWidth = width;
self_lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
self_lineRenderer.startColor = c1;
self_lineRenderer.endColor = c2;
AddPointToLineRenderer(0f, 0);
for (int i = 0; i <= resolution + 1; i++)
{
AddPointToLineRenderer((float)i / (float)(resolution) * 2.0f * Mathf.PI, i);
}
AddPointToLineRenderer(1f, resolution + 2);
}
void AddPointToLineRenderer(float angle, int index)
{
Quaternion pointQuaternion = Quaternion.AngleAxis (rotationAngle, Vector3.forward);
Vector3 pointPosition;
pointPosition = new Vector3(radius.x * Mathf.Cos (angle), radius.y * Mathf.Sin (angle), 0.0f);
pointPosition = pointQuaternion * pointPosition;
self_lineRenderer.SetPosition(index, pointPosition);
}
}