08-27-2018, 10:17 PM
(08-27-2018, 02:25 PM)_Aka_ Wrote:(08-27-2018, 03:39 AM)mythstified Wrote: It will find the nearest CP through a simple distance calc; it will InsertBefore that nearest CP with the position at the players current position; once it reaches the CP first found, it will remove the 'virtual' one I created to smooth out the path, and the original spline should look the same.Could you please send me the code that does that so I can see what goes wrong? Also, what Curvy version do you use? And if you have a ready made scene/project to test this, that would save some time.
Unfortunately it might be a bit tricky for me to engineer a test scene for this issue, but maybe you can look at my code and see what's wrong?
Code:
public void toggleFreeTurning()
{
if(isFreeTurning)
{
isFreeTurning = false;
findNearestCP();
//curvyOn = true;
} else
{
isFreeTurning = true;
//disable Curvy by removing it's active spline reference
splineCtrl.Spline = null;
//curvyOn = false;
}
}
public void findNearestCP()
{
//I've already precached all the cp positions in Start() into cpPositions, this LINQ will find the closest one
var cpClosest = cpPositions.OrderBy(t => (t.transform.position - VRTK_DeviceFinder.PlayAreaTransform().transform.position).sqrMagnitude)
.FirstOrDefault();
Debug.Log("===========" + cpClosest.name);
Debug.Log(cpClosest.transform.parent.name);
//follow the closest CP to it's parent so I know what Spline I'm playing with
CurvySpline parentSpline = cpClosest.transform.parent.GetComponent<CurvySpline>();
//Insert a new CP before the closest one on the exact player position (this way it forms a path to the "next" one)
parentSpline.InsertBefore(cpClosest.GetComponent<CurvySplineSegment>(), VRTK_DeviceFinder.PlayAreaTransform().transform.position);
//Not sure if necessary?
parentSpline.Refresh();
Debug.Log("TF:"+ parentSpline.GetNearestPointTF(VRTK_DeviceFinder.PlayAreaTransform().transform.position));
//put the closest CP's spline onto the controllers active spline
splineCtrl.Spline = parentSpline;
//This is my attempt to set the position of the spline to the nearest CP (which should now be the one I "insterBefore" above, ie. right on my player)
//It was added as an attempt to fix the offsetting, but didn't help
splineCtrl.Position = parentSpline.GetNearestPointTF(VRTK_DeviceFinder.PlayAreaTransform().transform.position);
}