How can I ensure a radius variable doesn't get reduced below 0?

For some reason when I'm using the mouse to change the radius in the Update, the minimum radius is -1 even if I set the range in the top of the script to be 0,50 and even if I check that the xradius is be 0 or higher it's still getting to -1.

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

[RequireComponent(typeof(LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;

    public bool circle = true;
    [Range(0, 50)]
    public float xradius = 15;
    [Range(0, 50)]
    public float yradius = 15;
    LineRenderer line;

    public float drawSpeed = 0.3f;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();

        line.positionCount = (segments + 1);
        line.useWorldSpace = false;
    }

    private void Update()
    {
        if (xradius >= 0)
        {
            if (Input.GetAxis("Mouse X") < 0)
            {
                xradius = xradius + 1;
                CreatePoints();
            }
            if (Input.GetAxis("Mouse X") > 0)
            {
                xradius = xradius - 1;
                CreatePoints();
            }
        }
    }

    private void CreatePoints()
    {
        if (circle == true)
            yradius = xradius;

        float x;
        float z;

        float change = 2 * (float)Math.PI / segments;
        float angle = change;

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

            line.SetPosition((int)i, new Vector3(x, 0.5f, z));

            angle += change;
        }
    }
}

In the Update I did:

if (xradius >= 0)

But yet when using the mouse for the minimum radius it set both xradius and yradius in the sliders to -1 not sure how can it be -1 if I set the range in both xradius and yradius to be 0,50

You can see in the screenshot in the Inspector in the right both Xradius and Yradius are -1

Radius -1

Answers 1

  • First of all, keep in mind that using a Range to limit a variable is something inspector-only! That means that if in the Update you did something like xradius = 100 it would work. Range is for convenience only and is in no way restricting.

    Based on your update, it seems that nothing happens when xradius drops below 0. That means by moving your mouse around, it can get a variety of values (including above 50 and below 0), but once it hits -1, it gets stuck, and there is no way for the value to change again.

    There are many ways to fix that, one of them is limiting the value of xradius the moment you change it, something like:

    if (xradius < 50) xradius++; // Ensures that xradius is always <= 50
    
    ...
    
    if (xradius > 0) xradius--; // Ensures that radius is always >= 0
    

    To avoid errors, as suggested by Bilkokuya, it would be better to xradius = 50 or xradius = 0 after you change its value, to ensure it is within range. Or alternatively use a min/max function xradius = Mathf.Max(Mathf.Min(50, xradius), 0)

    Or as a one liner, as pointed out by DMGregory, you can use Mathf.Clamp(radius + change, minRadius, maxRadius) like so:

    // where `change` in your case is either +1 or -1
    xradius = Mathf.Clamp(xradius +change, 0, 50);
    

Related Questions