Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling Connections with FollowSpline and Path Switching
#1
I have a couple of questions regarding how to handle connected splines with the FollowSpline script and switching between splines. 

My current prototype scene is setup as shown in the image below. There are two splines, the straight path and looping sub path, with these being connected through the last control point of the looped path and the 5th control point of the straight path. I then have an empty game object with the FollowSpline script attached to which my player object is parented. With the player object able to move freely in the X and Y axes.

[Image: Unity_Curvy_Question.jpg]

My questions relate to the game logic I am implementing to handle the player switching between the two paths in the scene.

Firstly I have a box collider (marked by the purple quad in my prototype scene)  which when the player model (with a rigidbody attached) collides with my player control script finds the spline that has the tag associated with the looped path and then tells the FollowSpline script to make this the spline to follow. This works relatively well, although the switch is rather sharp and I was wondering if there was a way to smooth/ease the switch so that it occurred more smoothly i.e. tell the FollowSpline script to slowly switch the spline paths?

Secondly, I was wondering if there was a way to have the FollowSpline script handle connections? I tried using the legacy SplineWalkerCon script which handled the connections fine but when it came to the switching, my player model would start on the looped path at the 5th or 6th control point rather than the first, which is obviously not ideal.

Any suggestions/advice would be appreciated.

Also thanks for the new SplineShape feature, it makes generating loops and spirals so much easier than having to roll my own code to generate them or build them by hand.

 
Reply
#2
Hi,

the SplineWalkerCon, though still working, will become obsolete once the new Networking featues are in (Connections will only be used to visually connect curves then). But even then using a network map might be a bit overkill unless you need pathfinding etc.

I would go the FollowSpline route: Create your custom controlle as a subclass of FollowSpline and overwrite some logic (Refresh()!) to manage your connections. E.g. if you move with the PingPong clamp option, you are able to detect the end of spline easily by checking if the direction changed. If so, check the last CP and if connected to another Spline, switch splines and add the delta movement to the new spline. In Pseudocode:

Code:
override Refresh()
{
MoveBy(ref tf, ref dir,...,CurvyClamping.PingPong);
if (dir==-1) { // <=this assumes you're normally moving forward only. If not you'll need to handle start of splines as well, but the concept remain the same
tf=1-tf;
dir=1;
// Find new Spline
var lastCP=spline[spline.Count-1];
var con=lastCP.ConnectionAny;
if (con!=null) // connected? then get new spline
spline=con.GetCounterpart(lastCP).Spline;
else
LetMeCrashOrDieOrElse();

}
}

Another topic is smooth transition between splines midway. One way would be to drag the player toward the final position where he should be. See the EndlessPlayer example. It doesn't use a FollowSpline based controller, but you'll get the trick. Basically uncouple direct movement for your player if he's in "switch Lane" mode and drag him toward the desired target position until he completed the transition. Another way would be to "move" on two splines and lerp the results while in "Switch" mode.

Does that help?
Reply
#3
(05-20-2014, 12:08 PM)'Jake' Wrote: Hi,

the SplineWalkerCon, though still working, will become obsolete once the new Networking featues are in (Connections will only be used to visually connect curves then). But even then using a network map might be a bit overkill unless you need pathfinding etc.

I would go the FollowSpline route: Create your custom controlle as a subclass of FollowSpline and overwrite some logic (Refresh()!) to manage your connections. E.g. if you move with the PingPong clamp option, you are able to detect the end of spline easily by checking if the direction changed. If so, check the last CP and if connected to another Spline, switch splines and add the delta movement to the new spline. In Pseudocode:



Code:
override Refresh()
{
MoveBy(ref tf, ref dir,...,CurvyClamping.PingPong);
if (dir==-1) { // <=this assumes you're normally moving forward only. If not you'll need to handle start of splines as well, but the concept remain the same
tf=1-tf;
dir=1;
// Find new Spline
var lastCP=spline[spline.Count-1];
var con=lastCP.ConnectionAny;
if (con!=null) // connected? then get new spline
spline=con.GetCounterpart(lastCP).Spline;
else
LetMeCrashOrDieOrElse();

}
}

Another topic is smooth transition between splines midway. One way would be to drag the player toward the final position where he should be. See the EndlessPlayer example. It doesn't use a FollowSpline based controller, but you'll get the trick. Basically uncouple direct movement for your player if he's in "switch Lane" mode and drag him toward the desired target position until he completed the transition. Another way would be to "move" on two splines and lerp the results while in "Switch" mode.

Does that help?


 

Hi Jake,

Thanks for the reply. I'll need to work through the pseudocode and see if I can convert it to working code (not quite completely adept at working in C# yet), but it'll give me a starting point. When you suggest making a subclass do you mean something like this?




Code:
public class FollowSpline
{}

public class Player_Movement : FollowSpline
{}


With regards to the smooth transition, I'll dig into the Endless runner example and see how I can adapt the concepts in that example . However I'm also intrigued by your suggestion of moving on two splines and lerping between them, would you be able to elaborate on that?
 

 
Reply
#4
Hi,

the pseudocode is almost working code, not much to change here:
Code:
public class FollowConnectedSplines : FollowSpline
{
public override Refresh()
{
Transform.position = Spline.MoveBy(ref mCurrentTF, ref Current.m_Direction, Speed * CurvyUtility.DeltaTime, Clamping);
if (Current.m_Direction==-1) { // <=this assumes you're normally moving forward only. If not you'll need to handle start of splines as well, but the concept remain the same
mCurrentTF=1-mCurrentTF;
Current.m_Direction=1;
// Find new Spline
var lastCP=Spline[Spline.Count-1];
var con=lastCP.ConnectionAny;
if (con!=null) // connected? then get new spline
Spline=con.GetCounterpart(lastCP).Spline;
}
}

With lerping between two splines I mean if your player switches splines, find the TF on the new spline (e.g. by calling GetNearestPointTF()) and then move on the new spline as you do on the former one and lerp between the two new positions until you have totally switched over. Never tried it, but should look much like the "Drag Toward" approach.
Reply
#5
(05-20-2014, 05:42 PM)'Jake' Wrote: Hi,

the pseudocode is almost working code, not much to change here:


Code:
public class FollowConnectedSplines : FollowSpline
{
public override Refresh()
{
Transform.position = Spline.MoveBy(ref mCurrentTF, ref Current.m_Direction, Speed * CurvyUtility.DeltaTime, Clamping);
if (Current.m_Direction==-1) { // <=this assumes you're normally moving forward only. If not you'll need to handle start of splines as well, but the concept remain the same
mCurrentTF=1-mCurrentTF;
Current.m_Direction=1;
// Find new Spline
var lastCP=Spline[Spline.Count-1];
var con=lastCP.ConnectionAny;
if (con!=null) // connected? then get new spline
Spline=con.GetCounterpart(lastCP).Spline;
}
}

With lerping between two splines I mean if your player switches splines, find the TF on the new spline (e.g. by calling GetNearestPointTF()) and then move on the new spline as you do on the former one and lerp between the two new positions until you have totally switched over. Never tried it, but should look much like the "Drag Toward" approach.


 

Thanks for the answer/update to the pseudocode, I've added it to my modified FollowSpline script, although I had to add public override void Refresh() to get it accepted by Unity.

My really stupid beginners question now is how to access/call this subclass? Do I call it in movement script attached to the player model i.e. when the code is triggered to switch the splines, replace my current method of finding the correctly tagged spline with a call to this subclass.

I Googled accessing subclasses in C#, but while I got the basic understanding of how they worked, trying to implement those methods in my code just seemed to get me nowhere fast.

 

 
Reply
#6
Subclass is another name for derived or inherited classes, try reading some docs about C# object orientation and inheritage (the Microsoft docs are hard to understand sometimes, but I'm sure there is tons of dedicated learning material floating around for free).

In short, your new script is a MonoBehaviour (because it's derived from FollowSpline which is derived from MonoBehaviour), so you can just add it to a GameObject.
Reply
#7
(05-21-2014, 09:48 AM)'Jake' Wrote: Subclass is another name for derived or inherited classes, try reading some docs about C# object orientation and inheritage (the Microsoft docs are hard to understand sometimes, but I'm sure there is tons of dedicated learning material floating around for free).

In short, your new script is a MonoBehaviour (because it's derived from FollowSpline which is derived from MonoBehaviour), so you can just add it to a GameObject.


 

Thanks for the reply. I'm slowly working my way through the documentation I can find on C# inheritance etc.

I think I've finally started to get a handle on things. However I'm now getting the following error 'Assets/Plane_Movement.cs(69,72): error CS0122: `FluffyUnderware.Curvy.shipMove.Current' is inaccessible due to its protection level', where Plane_Movement is my player movement script with the override class and shipMove is my modified FollowSpline script.

I'm guessing I haven't quite got a grasp of inheritance just yet. 


 

 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Spline Shape not centering around Spline Path tfunitydev 3 121 05-13-2021, 08:50 AM
Last Post: _Aka_
  Is it possible to generate an output spline after the conform path module? bennidhamma@gmail.com 3 277 03-21-2021, 06:37 PM
Last Post: _Aka_
  how to change the look direction of a character at end of path ninadpradhan 1 363 12-16-2020, 09:30 AM
Last Post: _Aka_
  Input Spline Path Material Option not suported Andrei 3 572 12-15-2020, 12:20 PM
Last Post: _Aka_

Forum Jump: