Reputation: 397
I have my model setup with wheel colliders and it drives fine. Everything is working well, including the position of the wheel mesh to simulate suspension. My only issue is that when I run the game, my wheels rotate 90 degrees on the Y axis and then roll along sideways. How can I rotate the mesh in the script so that it displays properly? I mainly only need position, but using wheel.getworldpose forces me to use Quaternion and Vector3. Can I do this without using Quaternion?
Heres an Image...
// Visual updates
void Update()
{
if (!driveable)
{
return;
}
// SETUP WHEEL MESHES
// Turn the mesh wheels
frontLeftWheelWrapper.localEulerAngles = new Vector3(0, 0, steerAngle);
frontRightWheelWrapper.localEulerAngles = new Vector3(0, 0, steerAngle);
// Wheel rotation
frontLeftWheelMesh.Rotate(0, 0, wheelFL.rpm / 60 * 360 * Time.deltaTime);
frontRightWheelMesh.Rotate(0, 0, wheelFR.rpm / 60 * 360 * Time.deltaTime);
rearLeftWheelMesh.Rotate(0, 0, wheelRL.rpm / 60 * 360 * Time.deltaTime);
rearRightWheelMesh.Rotate(0, 0, wheelRR.rpm / 60 * 360 * Time.deltaTime);
//Wheel Position
foreach (WheelCollider wheel in m_Wheels)
{
Quaternion q;
Vector3 p;
wheel.GetWorldPose(out p, out q);
// Assume that the only child of the wheelcollider is the wheel shape.
Transform shapeTransform = wheel.transform.GetChild(0);
shapeTransform.position = p;
shapeTransform.rotation = q;
}
Upvotes: 0
Views: 2754
Reputation: 392
I encountered a similar issue recently. I think the advantage of using the world pose is that it keeps you from having to handle all the rotations and reducing bugs and headache.
I have a workaround that involves adding a Vector3
to the wheel struct called startRotationPose
which stores the rotation of wheel as you typed in your editor e.g. 0, -90, 90
.
Then inside the foreach field, I have this:
wheel.wheelCollider.GetWorldPose(out position, out rotation);
// wheel.wheelModel.transform.position = position;
Vector3 colliderToEuler = rotation.eulerAngles;
float resolvedX = colliderToEuler.x - wheel.startRotationPose.x;
float resolvedY = wheel.wheelCollider.steerAngle + wheel.startRotationPose.y;
wheel.wheelModel.transform.localRotation = Quaternion.Euler(0, resolvedY, resolvedX);
And this worked fine. Notice that I put my x rotation in the z axis. This is due to the planes from the wheel mesh. So, it might differ in different scenarios. Hope it helps anyone else in the future.
Upvotes: 0
Reputation: 397
I finally solved this by commenting out shapetransform.rotation = q;
Then I used the original rotation in my script to rotate the wheels, and I used the wheel.getworldpose to update the position.
Upvotes: 0