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
  How to get length starting from one spline to connected nth spline Dragon-3623 1 1 05-14-2024, 04:52 PM
Last Post: _Aka_
  Curvy Line Renderer for UI Spline? gekido 3 6 04-04-2024, 12:56 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 6 03-28-2024, 10:08 PM
Last Post: _Aka_
Bug Changing spline connection in inspector causes splines to revert to defaults lacota 3 6 03-18-2024, 07:55 PM
Last Post: _Aka_

Forum Jump: