New Unity user here, playing around with Wheel Colliders.
I’ve made a simple “vehicle” consisting of a resized “Cube” GameObject with 4 empty child objects with WheelCollider components attached, one in each corner of the vehicle. The vehicle is situated on a plane:
I’ve the made a small script that sets the motorTorque on the two rear wheels, and the vehicle moves forward, as expected. However, it doesn’t move in a straight line, but turns slightly to the left. Everything is set up symmetrically on the vehicle, and the plane is level. Shouldn’t the vehicle then run in a straight line?
My controller script (C#):
using UnityEngine;
public class BasicCarController : MonoBehaviour
{
// Private member variables
private WheelCollider[] wheelColliders;
void Start()
{
wheelColliders = new WheelCollider[4];
wheelColliders[0] = gameObject.transform.Find("Wheels/FrontLeft").GetComponent<WheelCollider>();
wheelColliders[1] = gameObject.transform.Find("Wheels/FrontRight").GetComponent<WheelCollider>();
wheelColliders[2] = gameObject.transform.Find("Wheels/RearLeft").GetComponent<WheelCollider>();
wheelColliders[3] = gameObject.transform.Find("Wheels/RearRight").GetComponent<WheelCollider>();
}
void FixedUpdate()
{
wheelColliders[2].motorTorque = 15;
wheelColliders[3].motorTorque = 15;
}
}
The Project can be downloaded from
http://debian1.vegardw.com/WheelColliderTest/WheelColliderTest.zip
Webplayer showing the vehicle turning at
http://debian1.vegardw.com/WheelColliderTest/WebPlayer/WebPlayer.html