Posts: 9
Threads: 2
Joined: Dec 2013
Hi, i have a machine that moves along the path as in this video. (Without physics)
Is it possible to synchronize the rotation of the wheels and their turn? (Depending on the speed and steepness of the curve)
Any help would be appreciated.
Posts: 2,160
Threads: 95
Joined: Jun 2017
Hi,
Can you please explain me the logic of movement of the car? I see that it goes left and right of the path. Is it through user inputs?
If the car followed strictly the path, I would have suggested you to align the wheels with the spline's tangent. It might give a good result.
Posts: 9
Threads: 2
Joined: Dec 2013
The machine moves strictly on the spline. I managed to achieve the rotation of the wheels. It remains to realize the turn of the front wheels. Here's my video:
Can you tell me a sample code how to get a tangent to apply to turning the wheel?
Posts: 2,160
Threads: 95
Joined: Jun 2017
Off the top of my head:
Vector3 frontWheelLocalPosition = XXX; // The position in the spline's local space.
float frontWheelTf = spline.GetNearestPointTF(frontWheelLocalPosition );
Vector3 tangent = spline.GetTangent(frontWheelTf);
More info about how to use these methods is available in the method's documentation
Posts: 9
Threads: 2
Joined: Dec 2013
Thanks for help!
Wrote this script to get tangent.
Code:
Vector3 wheelLocalPos = splinePos.InverseTransformPoint(wheelFrontR.transform.position);
float frontWheelTf = spline.GetNearestPointTF(wheelLocalPos);
tang = spline.GetTangent(frontWheelTf);
wheelFrontR.transform.localEulerAngles = new Vector3(0.0f, tang.z * 30, 0.0f); // turning the wheel
Result
But the result was quite strange.
I think I'm getting the angle wrong. Help please, where is my mistake?
Posts: 2,160
Threads: 95
Joined: Jun 2017
In general, spline's data is expressed in spline's space, and you will need to convert them to global space when needed. So the tangent you get needs to be converted back to global space.
Then do this:
wheelFrontR.rotation = Quaternion.LookRotation(tangentInGlobalSpace, yourCarUp);
You might need to do an additional 90° rotation around the up vector, depending on the orientation of your mesh.
In general, when things go wrong, I advise you to debug things step by step, and visually. Use the Debug.DrawLine and similar methods to check the result at each significant step of your code, and I am sure you will rapidly find at which step things went wrong.
If after all this, you still can't manage to make the wheels align with the tangent, please send me a mini-project with everything setup, and I will take a look at it.
Posts: 9
Threads: 2
Joined: Dec 2013
Thank you! It worked! I used tangent incorrectly.
Final code (can someone use to anyone)
Code:
Vector3 wheelLocalPos = splinePos.InverseTransformPoint(wheelFrontR.transform.position);
float frontWheelTf = spline.GetNearestPointTF(wheelLocalPos);
tang = spline.GetTangent(frontWheelTf);
wheelFrontR.transform.rotation = Quaternion.LookRotation(tang, new Vector3(0, 1, 0)); // turn wheel
Posts: 2,160
Threads: 95
Joined: Jun 2017
Posts: 1
Threads: 0
Joined: Oct 2018
(10-30-2018, 10:15 AM)xcube Wrote: Thank you! It worked! I used tangent incorrectly.
Final code (can someone use to anyone)
Code:
Vector3 wheelLocalPos = splinePos.InverseTransformPoint(wheelFrontR.transform.position);
float frontWheelTf = spline.GetNearestPointTF(wheelLocalPos);
tang = spline.GetTangent(frontWheelTf);
wheelFrontR.transform.rotation = Quaternion.LookRotation(tang, new Vector3(0, 1, 0)); // turn wheel
Yes it really works ..
Posts: 9
Threads: 2
Joined: Dec 2013
It took a long time and I needed to use this script again.
I apply it to the car but get a strange result, the spline turns left, and the front wheels of the car turn the other way around to the right. I've tried a lot of options and deployed pacifiers and multiplied vectors by -1 , but still the wheels turn in the opposite direction.
How can I fix this?