Posts: 22
Threads: 5
Joined: Jul 2023
More precisely, hot to detect if current relative position is on bend and how strong the bend is?
If that makes sense?
I'm planning to implement bend leaning of my train cart that will have 2 parameters: speed and bend curvature.
Posts: 2,091
Threads: 88
Joined: Jun 2017
Hi
You can do that by retrieving the tangent of the spline at the position of the train cart, and compare it with the tangent at a point a little further (or behind) on the spline. Use the CurvySpline.GetTangent method.
Did this help?
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
Posts: 21
Threads: 6
Joined: Aug 2023
(07-30-2023, 05:14 PM)velikizlivuk Wrote: Will try, thank You!
Hey guys, just to add to this discussion I tried something like the below to get a variable for the lean angle to use at that point but I suppose there must be a better way if anyone has any ideas as I'm not sure the tf amount I'm using will always represent the same distance.
Vector3 Cornernow = Spline.GetTangent(mTF);
Vector3 Cornercomingup = Spline.GetTangent(mTF + 0.01f);
Debug.Log("lean angle " + (Cornernow.x-Cornercomingup.x));
Posts: 2,091
Threads: 88
Joined: Jun 2017
08-11-2023, 09:58 AM
(This post was last modified: 08-11-2023, 09:58 AM by _Aka_.)
Hi
TF is not proportional to distance. More about this here:
https://www.youtube.com/watch?v=rP0zuAEoVJw
It would be best to use GetTangentByDistance
The way you compute the angle is invalid in a lot of cases. You can use Unity's Vector3.Angle, or a custom method if Unity's method is not precise enough.
Please consider leaving a
review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Posts: 21
Threads: 6
Joined: Aug 2023
(08-11-2023, 09:58 AM)_Aka_ Wrote: Hi
TF is not proportional to distance. More about this here: https://www.youtube.com/watch?v=rP0zuAEoVJw
It would be best to use GetTangentByDistance
The way you compute the angle is invalid in a lot of cases. You can use Unity's Vector3.Angle, or a custom method if Unity's method is not precise enough.
Thanks so much for the tip! I'm now using this which seems to be working much better:
float mTFDistance = Spline.TFToDistance(mTF, CurvyClamping.Clamp);
Vector3 Cornernow = Spline.GetTangentByDistance(mTFDistance);
Vector3 Cornercomingup = Spline.GetTangentByDistance(mTFDistance + 0.5f);
float leanangle = Vector3.SignedAngle(Cornernow, Cornercomingup, Vector3.up);
Debug.Log("lean angle " + leanangle);
Posts: 2,091
Threads: 88
Joined: Jun 2017
You are welcome.
If and when you feel like it, please leave a review for the asset, that helps a lot.
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
I used 2 variables for storing tangents during Update method
private Vector3 previousTangent=Vector3.zero;
private Vector3 currentTangent=Vector3.zero;
and in Update:
// Check if we have current tangent and set as previous
if (currentTangent!=Vector3.zero)
{
previousTangent = currentTangent;
}
// Angle on Z axis to lean object on rail
sideLean = (Vector3.Angle(currentTangent - previousTangent, transform.forward) - 90) * (Speed/MaxSpeed)*MaxLeanFactor;
// Apply lean angle
this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, sideLean);
Speed is current speed of object on rail
MaxSpeed is maximum speed on rail
MaxLeanFactor is factor for making lean more or less
I'm not sure if that's working correctly since I discarded that feature.
Posts: 22
Threads: 5
Joined: Jul 2023
No wonder I removed feature since it was not working.
Up Dumping Time: does something similar but now I want to make it work.
How to apply rotation of object on rail since it is constantly corrected by movement on spline?
If Direction Dumping and Up Dumping Time are set to 0 rotating on Y or Z is always in line with spline?
Posts: 22
Threads: 5
Joined: Jul 2023
Figured it out.
Move leaning logic behind base.Update() in my Motor Update() component and:
this.gameObject.transform.localEulerAngles = new Vector3 (this.gameObject.transform.localEulerAngles.x, this.gameObject.transform.localEulerAngles.y, this.gameObject.transform.localEulerAngles.z+sideLean);