Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving object down or up the spline using gravity
#6
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.
Reply


Messages In This Thread
RE: Moving object down or up the spline using gravity - by velikizlivuk - 07-26-2023, 05:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to duplicate a spline with an offset Kapistijn 8 1,297 04-12-2026, 03:18 PM
Last Post: _Aka_
Smile Constant speed along a spline? tfishell 1 645 11-13-2025, 11:32 AM
Last Post: _Aka_
  Create Game Object Renaming Options rickgplus 1 925 09-23-2025, 09:33 AM
Last Post: _Aka_
  SplineController Ignores Follow-Up and Chooses Wrong Spline Josenildo 7 4,432 07-29-2025, 09:15 PM
Last Post: _Aka_

Forum Jump: