0
\$\begingroup\$

In this part when I'm changing the xradius slider it's also changing the yradius slider at the same time and same value. But I can't change the yradius when trying to change it's slider. I want to be able either change the xradius or yradius when the flag is true. Both will change to the same values but I want to be able to change also the yradius so it will change the xradius.

if (changeBothRadius)
        {
            yradius = xradius;
            xradius = yradius;
        }

I tried to add the xradius = yradius; but it's not working only when changing the xraiuds it's working.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 5;
    [Range(1, 50)]
    public float yradius = 5;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;

    private LineRenderer line;
    private List<float> radiusList = new List<float>();

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
        CreatePoints();
    }

    void Update()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;
        CreatePoints();
    }

    void CreatePoints()
    {
        if (changeBothRadius)
        {
            yradius = xradius;
            xradius = yradius;
        }

        float x;
        float z;

        float angle = 20;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, 0, z));

            angle += (360f / segments + 1);
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Remember, code is just a set of instructions that the computer follows literally. It doesn't read ahead to try to infer your intention.

So let's say xradius = 5 and yradius = 5 and you change the yradius slider to 7. The first command the CPU gets to is:

yradius = xradius;

So it dutifully follows this instruction, and undoes the change you made with the slider, setting yradius to 5, since that's the value of xradius. Only after it's done that does it reach this line:

xradius = yradius;

...which is now completely redundant because both variables already have the same value of 5.

In order to react the way you expect to either radius changing, it needs to have some memory of what the old values were, so it knows which one changed and can apply that one's value to the unchanged one.

If you're just changing these values in the inspector, then resetting the points every frame in Update() is a bit wasteful, and we can do it in OnValidate() instead to react only when an inspector value has changed in a way that might force us to re-generate our points:

Vector2 _cachedRadii;
Vector3[] _points;

void OnValidate() {
    if(line == null)
        line = GetComponent<LineRenderer>();

    line.startWidth = lineThickness;
    line.endWidth = lineThickness;

    bool regenPoints = false;

    if(_points == null || _points.Length != segments + 1) {
        _points = new Vector3[segments + 1];
        regenPoints = true;
    }

    if(xradius != _cachedRadii.x) {
        regenPoints = true;
        if(changeBothRadius)
           yradius = xradius;
    }
    if(yradius != _cachedRadii.y) {
        regenPoints = true;
        if(changeBothRadius)
           yradius = xradius;
    }

    if(regenPoints)
        CreatePoints();
}

void CreatePoints()
{
    float angle = 20;

    for (int i = 0; i < (segments + 1); i++)
    {
        float x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
        float z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

        _points[i] = new Vector3(x, 0, z);

        angle += (360f / segments + 1);
    }

    if(line.positionCount != _points.Length)
        line.positionCount = _points.Length;
    line.SetPositions(_points);

    // Remember the last radii we used to generate points.
    _cachedRadii.x = xradius;
    _cachedRadii.y = yradius;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.