public NearestLineData GetNearestLineData(Vector2 _position) { NearestLineData nearestLineData = new NearestLineData(gameObject); for(int i = 0;i < knots.Count-1;i++) { float distance = CalculateDistance(knots[i].position,knots[i+1].position,_position); Debug.Log(distance); if(distance < nearestLineData.distance) { nearestLineData.distance = distance; nearestLineData.begin = knots[i]; nearestLineData.end = knots[i+1]; nearestLineData.insertIndex = i+1; } } return nearestLineData; }
void AddKnot() { // calculate the nearest straight line NearestLineData nearestLine = new NearestLineData(null); Vector3 vx = MainCamera.ScreenToWorldPoint(Input.mousePosition); Vector2 worldCoords = new Vector2(vx.x,vx.y); foreach(GameObject _sp in Main.instance.splines){ NearestLineData nearestLineData = _sp.GetComponent<Spline>().GetNearestLineData(worldCoords); if(nearestLine.distance > nearestLineData.distance) nearestLine = nearestLineData; } if(nearestLine.distance < TypesConstants.closeDistanceToAddKnot) { nearestLine.spline.GetComponent<Spline>().InsertKnot(worldCoords ,0,0,0,nearestLine.spline,nearestLine.insertIndex); } }