- Home /
WheelCollider rotates mesh wrong
Hello,
I want to work on a racing game, currently i only started creating the very Basics and i already stumbled upon a annoying bug.
I have a WheelCollider and a mesh as a child on the Collider. I use GetWorldPose() from the wheelcollider to apply the wheelcollider Rotation to the child mesh. When i start the game, the z-axis always rotates 90°. I have no clue how to fix this :( Any help is highly appreciated :)
[1]: /storage/temp/92907-play.png
My script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SimpleCarController : MonoBehaviour { public List<AxleInfo> axleInfos; public float maxMotorTorque; public float maxSteeringAngle; public void ApplyLocalPositionToVisuals(WheelCollider collider) { if (collider.transform.childCount == 0) { return; } Transform visualWheel = collider.transform.GetChild(0); Vector3 position; Quaternion rotation; collider.GetWorldPose(out position, out rotation); visualWheel.transform.position = position; visualWheel.transform.rotation = rotation; } public void FixedUpdate() { float motor = maxMotorTorque * Input.GetAxis("Vertical"); float steering = maxSteeringAngle * Input.GetAxis("Horizontal"); foreach (AxleInfo axleInfo in axleInfos) { if (axleInfo.steering) { axleInfo.leftWheel.steerAngle = steering; axleInfo.rightWheel.steerAngle = steering; } if (axleInfo.motor) { axleInfo.leftWheel.motorTorque = motor; axleInfo.rightWheel.motorTorque = motor; } ApplyLocalPositionToVisuals(axleInfo.leftWheel); ApplyLocalPositionToVisuals(axleInfo.rightWheel); } } } [System.Serializable] public class AxleInfo { public WheelCollider leftWheel; public WheelCollider rightWheel; public bool motor; // is wheel attached to motor public bool steering; // does wheel apply steering force }
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How do I get my wheel mesh to rotate on the proper axis? 3 Answers
Rotating child doesn't move parent Rigidbody? 1 Answer
Perfect friction with WheelColliders 1 Answer
My car spins back?? Help! 0 Answers