Posts: 22
Threads: 5
Joined: Jul 2023
There is no gravity when moving objects along the spline. Can be achieved using input like in Rigidbody scene but I need the object to move by itself.
How to detect angle of object moving on spline (that uses motor controller since I need to have option to move object using some interaction buttons) so I can add velocity according to slope of the spline?
Scenario: player moves train along spline (motor controller) but train has stopped on slope and player does not apply input. I need that train to move down the slope by itself since there are no breaks on train.
Posts: 22
Threads: 5
Joined: Jul 2023
Problem, as far as I can see, is detecting vertical inclination angle of the train:
Vector3.SignedAngle(Vector3.up, this.transform.forward, Vector3.up)
and after that what is next Segment according to that angle, train position on Spline and Spline orientation.
Anyone knows how to achieve this ^^^ ?
Posts: 2,091
Threads: 88
Joined: Jun 2017
Hi Velikizlivuk
Sorry for not answering yet. I should be able to provide an answer in about 12 hours.
Sorry again, and thanks for your patience
Please consider leaving a
review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Posts: 2,091
Threads: 88
Joined: Jun 2017
Hi
I modified the class to implement a simple slope induced speed:
Code:
public class E10_MotorController : SplineController
{
[Section("Motor")]
public float MaxSpeed = 30;
public float SlopeFactor = 10;
protected override void Update()
{
float axis = Input.GetAxis("Vertical");
float slopeInducedSpeed = Mathf.Abs(GetTangent(RelativePosition).y) * SlopeFactor;
Speed = Mathf.Abs(axis) * MaxSpeed + slopeInducedSpeed;
MovementDirection = MovementDirectionMethods.FromInt((int)Mathf.Sign(axis));
base.Update();
}
}
I hope this helped
Have a nice day
Please consider leaving a
review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Posts: 22
Threads: 5
Joined: Jul 2023
07-25-2023, 11:10 PM
Will try, thanks!
Posts: 22
Threads: 5
Joined: Jul 2023
I ended up using this:
Code:
public class Cart_MotorController : SplineController
{
[Section("Motor")]
public float MaxSpeed = 30;
public float RailSlopeFactor = 10;
[SerializeField] private float slopeInducedSpeed = 0;
[SerializeField] private float slopeInclination = 0;
private float axis = 0;
[SerializeField] private bool isPlayerInCart = false;
protected override void FixedUpdate()
{
axis = Input.GetAxis("Vertical");
slopeInclination = GetTangent(RelativePosition).y; // minus forward slope, plus backward slope
if (slopeInclination < -0.05f || slopeInclination > 0.05f)
{
slopeInducedSpeed = Mathf.Abs(slopeInclination) * RailSlopeFactor;
}
if (isPlayerInCart)
{
MovementDirection = MovementDirectionMethods.FromInt((int)Mathf.Sign(axis));
}
else
{
axis = 0;
MovementDirection = MovementDirectionMethods.FromInt((int)Mathf.Sign(-slopeInclination));
}
Speed = Mathf.Lerp (Speed, (Mathf.Abs(axis) * MaxSpeed)+slopeInducedSpeed, Time.fixedDeltaTime*1.5f);
if (Speed<0.01f)
{
Speed = 0.0f;
}
base.FixedUpdate();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isPlayerInCart = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isPlayerInCart = false;
}
}
}
Needs more polishing but it works as inteded.
I'm using rigidbody on my rail cart and player so using FixedUpdate as General/Update in and Kimenatic Rigidbody as General/Target component in Motor component.
Posts: 2,091
Threads: 88
Joined: Jun 2017
Thanks for sharing your solution with everyone else, appreciated.
Please consider leaving a
review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.