Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Control of an object that uses a spline
#1
If I have an object using a Spline Controller for movement, is there a way to remove the object from the spline's movement control temporarily or permanently?  For example, suppose I've got a ship using a spline for movement but want to evade a shot by the player with some other movement algorithm.  I've tried the following ways but have issues with each of them, I'm hoping I'm just missing something. Smile  I'm doing these when the controller raises the OnEndReached event for the spline.

  • Stop the spline controller - when I try to change the transform position of the object, the spline controller still makes it stay on the path.
  • Disable the spline controller in code - when I try to change the transform position of the object, the spline controller still makes it stay on the path.  Maybe because I'm trying to do the movement in the same frame as when I disable the controller?
  • Remove the current spline from the controller (i.e. controller.Spline = null) - This allows me to change the object's transform position, but it makes the controller angry and throws an error... I'm assuming this occurs because the spline is technically still in use.  Details below:

NullReferenceException: Object reference not set to an instance of an object
FluffyUnderware.Curvy.Controllers.SplineController.MovementCompatibleGetPosition (FluffyUnderware.Curvy.Controllers.SplineController controller, FluffyUnderware.Curvy.CurvyPositionMode positionMode, FluffyUnderware.Curvy.CurvySplineSegment& controlPoint, System.Boolean& isOnControlPoint, System.Single clampedPosition) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:603)
FluffyUnderware.Curvy.Controllers.SplineController.InvokeEventHandler (FluffyUnderware.Curvy.Controllers.CurvySplineMoveEvent event, FluffyUnderware.Curvy.Controllers.CurvySplineMoveEventArgs eventArgument, FluffyUnderware.Curvy.CurvyPositionMode positionMode, FluffyUnderware.Curvy.CurvySplineSegment& postEventsControlPoint, System.Boolean& postEventsIsControllerOnControlPoint, System.Single& postEventsControlPointPosition) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:937)
FluffyUnderware.Curvy.Controllers.SplineController.HandleReachingNewControlPoint (FluffyUnderware.Curvy.CurvySplineSegment controlPoint, System.Single controlPointPosition, FluffyUnderware.Curvy.CurvyPositionMode positionMode, System.Single currentDelta, System.Boolean& cancelMovement, FluffyUnderware.Curvy.CurvySplineSegment& postEventsControlPoint, System.Boolean& postEventsIsControllerOnControlPoint, System.Single& postEventsControlPointPosition) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:921)
FluffyUnderware.Curvy.Controllers.SplineController.EventAwareMove (System.Single distance) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:786)
FluffyUnderware.Curvy.Controllers.SplineController.Advance (System.Single speed, System.Single deltaTime) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:494)
FluffyUnderware.Curvy.Controllers.CurvyController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/Curvy/Curvy/Controllers/CurvyController.cs:675)
FluffyUnderware.Curvy.Controllers.SplineController.InitializedApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/Curvy/Curvy/Controllers/SplineController.cs:538)
FluffyUnderware.Curvy.Controllers.CurvyController.ApplyDeltaTime (System.Single deltaTime) (at Assets/Standard Assets/Curvy/Curvy/Controllers/CurvyController.cs:1014)
FluffyUnderware.Curvy.Controllers.CurvyController.Update () (at Assets/Standard Assets/Curvy/Curvy/Controllers/CurvyController.cs:605)


Any thoughts would be appreciated.  Thanks for your help.
Reply
#2
(09-14-2021, 04:24 PM)ricke Wrote:
  • Disable the spline controller in code - when I try to change the transform position of the object, the spline controller still makes it stay on the path.  Maybe because I'm trying to do the movement in the same frame as when I disable the controller?


This one is supposed to work. Maybe you execute the code disabling the spline controller component after said controller run its update method, modifying the transform.
If what I said does not apply on your use case, can you send me a simple reproduction case please?
Thanks, and have a nice
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
After some more testing, it appears to operate as suspected... basically, I need to execute my code sometime after the current frame I disable the spline controller.  

So this doesn't work:

Code:
    private void PathEndReached(CurvySplineMoveEventArgs arg0)
    {
        _controller.enabled = false;
        transform.position = new Vector3(960, 1080);
    }

But this does work:

Code:
    private void PathEndReached(CurvySplineMoveEventArgs arg0)
    {
        _controller.enabled = false;
        StartCoroutine("MoveToTop");
    }

    IEnumerator MoveToTop()
    {
        yield return new WaitForSeconds(0.1f);
        transform.position = new Vector3(960, 1080);
    }

That makes sense to me.  If there's a better way to do the above, please let me know.  Thanks again for your help.
Reply
#4
As long as your frame rate is higher that 10 FPS, the duration of a frame is less than 0.1s, meaning that a WaitForSeconds(0.1f) will delay the execution for at least a frame, thus the problem. The solution you found seems good to me
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#5
To finish out the waiting for a single frame, I found this in the Unity forum:

https://forum.unity.com/threads/how-to-w...n-c.24616/

TLDR - Have your coroutine do a "yield return null", which accomplishes the feat without the worry of FPS time.  Indeed, my new method works quite nicely.

Code:
IEnumerator MoveToTop()
{
    yield return null;
    transform.position = new Vector3(960, 1080);
}

Thanks again for your help, I appreciate it.
Reply
#6
You are welcome
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with spline prefab topquarkone 3 80 09-15-2021, 08:47 PM
Last Post: _Aka_
  Change to Random Spline on End Reached Event ricke 10 191 09-14-2021, 12:31 AM
Last Post: ricke
  Split existing segment(s) with additional control points KStress71 1 73 09-09-2021, 05:46 PM
Last Post: _Aka_
  Spline Controller Flickering blabz2007 6 217 09-03-2021, 09:29 AM
Last Post: _Aka_

Forum Jump: