0
\$\begingroup\$
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.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Myself, I'd do this in OnValidate if these variables are meant to be tuned in the editor. This gets called only when loading or editing the component, so it's ideal for this kind of reactive fix-up during tuning.

If these parameters can be set by scripts in-game, I'd have them call a SetRadii(x, y) method or something similar that can handle applying the changes on demand, rather than polling for changes every frame in your update loop.

This way, we don't need our prev variables at runtime at all - they can live only in the editor:

#if UNITY_EDITOR
float prevXRadius, prevYRadius;
int prevSegments;

void OnValidate() {
    // Can't set up our line if the user hasn't connected it yet.
    if (line == null)
        return;

    // If any values have changed, update our points.
    if(xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments) {
        CreatePoints();

        // Cache our most recently used values.
        prevXRadius = xRadius;
        prevYRadius = yRadius;
        prevSegments = segments;
    }
}   
#endif
\$\endgroup\$
0
\$\begingroup\$

This is working. Is that a good and logic 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;
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .