When i run my game for some reason two lines show up in the hierarchy every time i drag my mouse in order to create a line. I have also noticed that occasionally it kinda works as i expect where it only creates two copies the first time and then create a single line every time after the first. Likely i’m just editing the script and some stuff happens and it figures something out… i honestly have no idea. Other than that it looks fine and having two lines doesn’t seem to break anything, but i have a feeling it will in the future so i wanna get it fixed.
i want to stress that the attached image shows the result of me dragging my mouse across the screen once.
public class ToggleInputNode : MonoBehaviour
{
[SerializeField] GameObject _linePrefab;
[SerializeField] float _radiusToCreateLine = 1f;
private bool _lineCreated = false;
List<GameObject> _lines = new List<GameObject>();
private void OnMouseDrag()
{
//Create a Line if the User drags the mouse a certain range
if (DistanceFromCenter(_radiusToCreateLine) && !_lineCreated)
{
Debug.Log("There are " + _lines.Count + " lines");
_lines.Add(Instantiate(_linePrefab, transform.position, Quaternion.identity));
_lineCreated = true;
}
}
private void OnMouseExit()
{
_lineCreated = false;
}
private bool DistanceFromCenter(float r)
{
//Calculate distance between the node and the mouse
Vector2 nodePos = new Vector2(transform.position.x, transform.position.y);
Vector2 mousePos = GetMousePosition();
float distance = Vector2.Distance(nodePos, mousePos);
if (distance >= r)
return true;
return false;
}