06-26-2016, 12:45 PM
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:
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();
}
}