Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help to achieve reverse spline walk

I recently downloaded Unity and then last week got the Curvy 2 asset, seems like a great one!

I am trying to use splines to make a 2d game plaformer level with something like a roller-coaster. I was thinking using the tangent for acceleration of speed could be a nice effect. I tried to  change the MotorController.cs example a little bit. The speed increases when going downwards, and decreases when "climbing" a hill nicely, but when it goes below zero it does not go backwards. 

See this youtube video for how it looks:

My approach might be wrong? Below is the code I have used:

   public class MotorController : SplineController
       public float MaxSpeed = 30.0f;
       public float acceleration = 3.0f;

       protected override void Update()
           Vector3 tangent = Spline.GetTangentFast(RelativePosition);
           Vector3 orientation = Spline.GetOrientationUpFast(RelativePosition);

           Debug.DrawLine(transform.position, transform.position + tangent * 6.0f,;
           Debug.DrawLine(transform.position, transform.position + orientation * 6.0f,;            

           //Debug.Log("tangent: " + tangent);
           float someAcc = tangent.magnitude * (acceleration * Time.deltaTime) * Mathf.Sign(tangent.y)*-1.0f;
           //Debug.Log("Acc: " + someAcc);

           Speed += someAcc;

           //Speed = Input.GetAxis("Vertical") * MaxSpeed;
           Debug.Log("Speed: " + Speed);
What you want to know is this piece of information: "Note that speed changes the movement direction only if it was “0” before!" I missed to update the docs (just did it!), so it was only present in the changelog - sorry.

So you'll need to change your code like

float newSpeed=Speed+someacc;
if (Mathf.Sign(Speed)!=Mathf.Sign(newSpeed))

I agree that this is somewhat odd, but it was the most reliable way to solve a problem when moving over several connected splines. It's most likely that this behaviour will drop in the future in favor of a more sophisticated solution, though.
If you don't like that behaviour, just override the Speed property in your custom controller and remove the "m_Speed==0 && " condition in the property's setter!
Hello Jake, thank you. That worked like a charm! Smile

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I find the point on the spline after applying with offset angle+radius? Chanon 1 3 10-01-2022, 11:50 AM
Last Post: _Aka_
  Scale Deform Mesh Along Spline? / aligning colliders MamasToast 6 33 09-25-2022, 08:47 PM
Last Post: _Aka_
  How can I remove one segment of the road that I made by spline? Chanon 3 7 09-14-2022, 04:44 PM
Last Post: _Aka_
  Force spline normal to be always world up GameJazz 12 22 08-08-2022, 01:20 PM
Last Post: _Aka_

Forum Jump: