Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wheel control at the car
#1
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.
Reply
#2
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.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
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?
Reply
#4
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
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
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?
Reply
#6
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.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#7
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
Reply
#8
Thanks for sharing
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#9
(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 ..
Reply
#10
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?

   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoiding runtime GC allocations on control point position change Ell223 8 18 02-24-2024, 10:43 AM
Last Post: _Aka_
  Connection "next' control point jh092 3 15 11-22-2023, 11:47 AM
Last Post: _Aka_
  Custom Control Nodes Lupos 3 14 05-27-2023, 09:05 PM
Last Post: _Aka_
  How do I get the closest Spline Control Point Lupos 2 16 05-12-2023, 01:59 AM
Last Post: Lupos

Forum Jump: