- Home /
How do I get my wheel mesh to rotate on the proper axis?
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...
And heres the code Im using to update my wheel position and rotation...
// 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;
}
Answer by JV1587 · Jun 02, 2018 at 05:44 AM
The easiest fix I have seen is to 1. Create an empty game object, 2. align empty's rotation with the car body (as in the z axis is perpendicular to the side of the car). 3. Take the wheel mesh and make it the child of the empty game object. 4. Use those newly created objects as the wheel mesh. In my experience, the way the wheel spins has to do with how the axes have been assigned on import.
So, making an object the child of an empty object allows you to essentially rotate the axes on your child without actually moving/rotating the child
Answer by naveenkumark374 · Mar 06, 2018 at 04:43 PM
same problem man please help me may be you found that solution please
Answer by echozulu · Jul 17, 2021 at 02:43 AM
I ended up having this issue as well. I eventually solved it in the following manner...
Created a top-level empty game object as suggested
Added colliders empty game object and put all wheel colliders in game objects
Added a wheels game object parallel to colliders and rotated the y-axis 90 degrees so the wheels looked correct; moved wheels to proper locations
Each Wheel has its own class.. added a rotationDelta Quaternion to adjust the wheel transform during update. The class needs both the mesh and collider transform.
void Start() { wheelTransform = GetComponentInChildren<MeshRenderer>().GetComponent<Transform>(); rotationDelta = Quaternion.FromToRotation(wheelColliderTransform.transform.forward, wheelTransform.transform.forward); } void Update() { wheelCollider.GetWorldPose(out Vector3 pos, out Quaternion rot); wheelTransform.position = pos; wheelTransform.rotation = rot * rotationDelta; }
Your answer
Follow this Question
Related Questions
Wheel is wrong rotation 0 Answers
Why does my car re-align after drift into the original direction? 0 Answers
WheelCollider rotates mesh wrong 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers