07-26-2023, 05:52 PM
I ended up using this:
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.
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.