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);
}
}
}
I tried this, At the top I added :
private float prevxRadius = -1.0f;
private float prevyRadius = -1.0f;
private float prevSegments = -1.0f;
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();
}
The main goal is to change in real time in editor mode and runtime the values of the xradius,yradius and the segments.