Simple way of getting wheel mesh to rotate with wheel colliders?

So I’m getting the hang of making vehicles generally now I think but one thing that’s been frustrating lately is getting the wheels to turn with the colliders. I’m looking up various options and this one I found on this topic seemed to be the most easy to understand but despite following the instructions posted nothing seems to have happened.

using UnityEngine;
using System.Collections;

public class wheel : MonoBehaviour {

    public WheelCollider wheelC;

    private Vector3 wheelCCenter;
    private RaycastHit hit;

    void Start () {

    }

    void Update () {
        wheelCCenter = wheelC.transform.TransformPoint(wheelC.center);

        if ( Physics.Raycast(wheelCCenter, -wheelC.transform.up, out hit, wheelC.suspensionDistance + wheelC.radius) ) {
            transform.position = hit.point + (wheelC.transform.up * wheelC.radius);
        } else {
            transform.position = wheelCCenter - (wheelC.transform.up * wheelC.suspensionDistance);
        }
    }
}

Now one thing that was mentioned that the person who posted this code is suggesting is that he had empty gameobjects which I haven’t tried yet, I’ll give it a shot and see what happens but I don’t have a clue as to why my meshes aren’t rotating.

If putting the script on an empty gameobject doesn’t work, It’d be great if someone could help me with this particular bit of code. Or perhaps I should just take a look at the other code out there and try them out instead?

1 Like

Oh, I also just tried this script from a video here.

The results were pretty damn weird.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wheelrotate : MonoBehaviour {

    public WheelCollider wheelFL;
    public WheelCollider wheelFR;
    public WheelCollider wheelBL;
    public WheelCollider wheelBR;

    public Transform wheelFLTrans;
    public Transform wheelFRTrans;
    public Transform wheelBLTrans;
    public Transform wheelBRTrans;


    void FixedUpdate ()

    {
        wheelBR.motorTorque = GetComponent<VehicleController>().maxMotorTorque * Input.GetAxis ("Vertical");
        wheelBL.motorTorque = GetComponent<VehicleController>().maxMotorTorque * Input.GetAxis ("Vertical");

        wheelFL.steerAngle = GetComponent<VehicleController>().maxSteeringAngle * Input.GetAxis ("Horizontal");
        wheelFR.steerAngle = GetComponent<VehicleController>().maxSteeringAngle * Input.GetAxis ("Horizontal");
    }

    void Update ()

    {
        wheelFLTrans.Rotate (wheelFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
        wheelFRTrans.Rotate (wheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
        wheelBLTrans.Rotate (wheelBL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
        wheelBRTrans.Rotate (wheelBR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
    }





}
1 Like

… Just went and fixed it myself, found this answer on the subject.

I switched the code to the Y axis like this and everything is working now, hurray! :smile:

    void Update ()

    {
        wheelFLTrans.Rotate (0, wheelFL.rpm / 60 * 360 * Time.deltaTime, 0);
        wheelFRTrans.Rotate (0, wheelFR.rpm / 60 * 360 * Time.deltaTime, 0);
        wheelBLTrans.Rotate (0, wheelBL.rpm / 60 * 360 * Time.deltaTime, 0);
        wheelBRTrans.Rotate (0, wheelBR.rpm / 60 * 360 * Time.deltaTime, 0);
    }

I’ve also noticed something very unusual thanks to this code! it seems that my front wheels are rotating one way and my back wheels the other which might explain why I’ve been having tons of stability problems.

1 Like