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

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: https://www.youtube.com/watch?v=XKrNKKiSY9M

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

Code:
   public class MotorController : SplineController
   {
       [Section("Motor")]
       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, Color.red);
           Debug.DrawLine(transform.position, transform.position + orientation * 6.0f, Color.blue);            

           //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);
           base.Update();
       }
   }
Reply
#2
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

Code:
float newSpeed=Speed+someacc;
if (Mathf.Sign(Speed)!=Mathf.Sign(newSpeed))
{
Speed=0;
Speed=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!
Reply
#3
Hello Jake, thank you. That worked like a charm! Smile
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Spawn object on Curvy Spline. ktpttd 3 103 11-20-2019, 01:38 PM
Last Post: _Aka_
  Dummy spline points hnngaf 1 65 11-17-2019, 06:01 PM
Last Post: _Aka_
  Refresh spline doesn't seem to be working dazz777 4 139 11-13-2019, 11:13 PM
Last Post: _Aka_
  Same Spline Extrusion with variable width MarioBag 5 811 11-04-2019, 01:56 PM
Last Post: _Aka_

Forum Jump: