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.