0

I am currently struggling with wheel colliders in Unity on my car. I have tried different code and cars, even made a simple car in Unity. But nothing works as I would have it.

As seen on the pictures, my wheels keep searching into the center on my car and the wheels don't drive around themselves.

Image of my car controller:

Image of my wheel collider:

Image of my wheels:

Image of my wheels in the center of my car:

using UnityEngine;

public class CARController : MonoBehaviour
{

    public void GetInput()
    {
        m_horizontalInput = Input.GetAxis("Horizontal");
        m_verticalInput = Input.GetAxis("Vertical");
    }

    private void Steer()
    {
        m_steeringangle = maxSteeringAngle * m_horizontalInput;
        frontDriverW.steerAngle = m_steeringangle;
        frontpassengerW.steerAngle = m_steeringangle;
    }

    private void Accelerate()
    {
        frontDriverW.motorTorque = m_verticalInput * motorForce;
        frontpassengerW.motorTorque = m_verticalInput * motorForce;
    }

    private void UpdateWheelPoses()
    {
        UpdateWheelPose(frontDriverW, frontDriverT);
        UpdateWheelPose(frontpassengerW, frontpassengerT);
        UpdateWheelPose(rearDriverW, rearDriverT);
        UpdateWheelPose(rearPassengerW, rearpassengerT);
    }

    private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
    {
        Vector3 _pos = _transform.position;
        Quaternion _quat = _transform.rotation;

        _collider.GetWorldPose(out _pos, out _quat);

        _transform.position = _pos;
        _transform.rotation = _quat;
    }

    private void FixedUpdate()
    {
        GetInput();
        Steer();
        Accelerate();
        UpdateWheelPoses();

    }

    private float m_horizontalInput;
    private float m_verticalInput;
    private float m_steeringangle;

    public WheelCollider frontDriverW, frontpassengerW;
    public WheelCollider rearDriverW, rearPassengerW;
    public Transform frontDriverT, frontpassengerT;
    public Transform rearDriverT, rearpassengerT;
    public float maxSteeringAngle = 30;
    public float motorForce = 50;

}

Above here is the code of my engine to the car.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You seem to be missing the engine code from your question. – LSM07 Nov 24 '19 at 21:33
  • 2
    You mean despide the fact that they also are not even close to the positions where you would expect a wheel? ^^ since these wheels are nested under some parent objects: do these have an offset to the car maybe? Do you move them somewhere? Where are the pivot points of these parents, their children(colliders) and the car? Are there any unexpected offsets between them? – derHugo Nov 25 '19 at 05:58
  • the pivot points is in the middle of the car, how do I change that, or can I make the wheels spin around their center instead? – Frederik Niebling Nov 27 '19 at 08:46

1 Answers1

0

This usually happens when the wheel mesh axis is in the wrong place when using Pivot and local rotation below the menu options in the editor.

Try creating a cylinder and position it where the wheel is at. Then remove its renderer and filter and parent the actual wheel of the cylinder transform. Drag the cylinder as the wheel transform in inspector.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dennis Maina
  • 11
  • 1
  • 5