Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving object down or up the spline using gravity
#1
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.
Reply
#2
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 ^^^ ?
Reply
#3
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.
Reply
#4
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.
Reply
#5
Smile 
Will try, thanks!
Reply
#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
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  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_
  GO can't fit end of the spline GameDeveloperek4123 3 14 03-04-2024, 11:06 AM
Last Post: _Aka_

Forum Jump: