The script is working fine in runtime, but I’m not sure if it’s a good way to call CreatePoints in the Update.
How else can I make that it will change the circle of the radius size width in the Update/Realtime ?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
[Range(0, 50)]
public int segments = 50;
[Range(0, 5)]
public float xradius = 5;
[Range(0, 5)]
public float yradius = 5;
[Range(0.1f, 5f)]
public float width = 0.1f;
LineRenderer line;
void Start()
{
line = gameObject.GetComponent<LineRenderer>();
line.positionCount = segments + 1;
line.widthMultiplier = width;
line.useWorldSpace = false;
CreatePoints();
}
private void Update()
{
CreatePoints();
}
void CreatePoints()
{
line.widthMultiplier = width;
float x;
float y;
float z;
float angle = 20f;
for (int i = 0; i < (segments + 1); i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
line.SetPosition(i, new Vector3(x, 0f, y));
angle += (380f / segments);
}
}
}
Seems almost reasonable, but it would be much better if you just keep other variables for radius and segment, and if they don’t change each frame, don’t do the recalculate.
private float prevxRadius = -1.0f;
and
if (xradius == prevxRadius) // and also check yradius and segment
return;
prevxRadius = radius;
// here now do create...
In this case it is acceptable to test for equality in floating points because you are assigning it, not calculating it from another set of values.
Then in the Update , but it’s never get to the CreatePoints it’s only get to the first return no matter what values I change in the inspector.
private void Update()
{
if (xradius == prevxRadius) // and also check yradius and segment
return;
prevxRadius = xradius;
if (yradius == prevyRadius) // and also check yradius and segment
return;
prevyRadius = yradius;
if (segments == prevSegments) // and also check yradius and segment
return;
prevSegments = segments;
CreatePoints();
}
Then I tried this in the Update but it’s not working either it’s calling the CreatePoints after the prevxRadius = xradius; when I change the xradius value but the prev and current values are the same.
private void Update()
{
if (xradius == prevxRadius) // and also check yradius and segment
return;
prevxRadius = xradius;
CreatePoints();
if (yradius == prevyRadius) // and also check yradius and segment
return;
prevyRadius = yradius;
CreatePoints();
if (segments == prevSegments) // and also check yradius and segment
return;
prevSegments = segments;
CreatePoints();
}
This is a working solution. Is that a good solution ?
private void Update()
{
if (xradius != prevxRadius) // and also check yradius and segment
{
CreatePoints();
}
prevxRadius = xradius;
if (yradius != prevyRadius) // and also check yradius and segment
{
CreatePoints();
}
prevyRadius = yradius;
if (segments != prevSegments) // and also check yradius and segment
{
CreatePoints();
}
prevSegments = segments;
}