How can I use the enum modes and the rest of properties of the script ? They are not working good

If I’m removing the line circleheight = CircleHeight. None; then I can’t change the circle height at all it’s locked. If the line exists then when changing to Center it will change it to None but then I can change the circle height and if I’m changing it to Top or Bottom I also can change the circle height. But without this line, the height is locked on 0 when the bool flag draw is true.

I’m using the None to be able to change the drawn circle height but if the Draw flag is true the circleheight is locked on 0 I can’t change it in run time only if I’m changing the enum modes to Top or Bottom.

It’s all messed up.

I need to be able to change the circle height and to be able to change both X and Y radiuses at the same time if the flag changeBothRadius is true. but the enum is messed and the flag changeBothRadius if it’s true it does nothing I still can change the X and Y radiuses each one by itself.

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

[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
   public enum CircleHeight
   {
       Center, Bottom, Top, None
   };

   public enum AnimateCircleStates
   {
       Once, Pingpong
   };

   public CircleHeight circleheight;
   [Range(0, 50)]
   public int segments = 50;
   [Range(1, 50)]
   public float xradius = 1.5f;
   [Range(1, 50)]
   public float yradius = 1.5f;
   [Range(-10, 10)]
   public float height = 0;
   public bool changeBothRadius = false;
   [Range(0.1f, 2)]
   public float lineThickness = 0.1f;
   public bool minimumRadius = false;
   public bool draw = false;
   public bool animateCircle = false;
   public float animationSpeed = 0.5f;
   public AnimateCircleStates animatecircle;

   private LineRenderer line;
   private Renderer renderer;
   private float Bottom;
   private float Top;
   private float Center = 0;
   private float t = 0f;
   private float tGoal;
   private bool animateOnce = false;
   private bool animatePingpong = false;

   void Start()
   {
       circleheight = CircleHeight.Center;
       //animatecircle = AnimateCircleStates.Once;

       line = gameObject.GetComponent<UnityEngine.LineRenderer>();
       line.positionCount = segments + 1;
       line.useWorldSpace = false;
       renderer = gameObject.GetComponent<Renderer>();

       Bottom = transform.InverseTransformPoint(renderer.bounds.min).y + 0.1f;
       Top = transform.InverseTransformPoint(renderer.bounds.max).y + 0.1f;
   }

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

       if (draw)
       {
           line.enabled = true;
           CreatePoints();
       }
       else
       {
           line.enabled = false;
       }
   }

   void CreatePoints()
   {
       float x;
       float z;

       float angle = 20;

       switch (circleheight)
       {
           case CircleHeight.Center:
               float centerT = Mathf.InverseLerp(Bottom, Top, Center);

               t = 2f - centerT;
               tGoal = 3f;
               height = Center;
               circleheight = CircleHeight.None;
               break;

           case CircleHeight.Bottom:
               t = 0f;
               tGoal = 1f;
               height = Bottom;
               break;

           case CircleHeight.Top:
               t = 1f;
               tGoal = 2f;
               height = Top;
               break;

           case CircleHeight.None:
               break;
       }

       if (animateCircle)
           AnimateCircle();

       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, height, z));

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

   private void AnimateCircle()
   {
       switch (animatecircle)
       {
           case AnimateCircleStates.Once:
               animateOnce = true;
               break;
           case AnimateCircleStates.Pingpong:
               animatePingpong = true;
               break;
       }

       if (animateOnce)
       {
           // prevent t from exceeding tGoal
           if (t > tGoal)
           {
               t = tGoal;
           }

           // end animation when t reaches goal
           if (t == tGoal)
           {
               animateOnce = false;
           }

           // advance height according to t
           height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f));

           // advance height according to time & speed
           t += animationSpeed * Time.deltaTime;
       }

       if (animatePingpong)
       {
           // advance height according to t
           height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f));

           // advance height according to time & speed
           t += animationSpeed * Time.deltaTime;
       }
   }
}

The main goal is to be able to draw a circle and control it with the properties at run time in the Inspector.

It’s hard to grasp what you’re doing, but when I run the code above I get a live circle created no problem.

There is one minor bug I fixed: before the for() loop on line 114, do this:

         line.positionCount = segments + 1;

So when you change the segment count, it works properly.

There are a lot of controls on there as well as implied heights and numbers I’m not sure what they mean. To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like