Wheel Mesh Not rotating with Wheel Collider

1.2k Views Asked by At

So I'm working on a racing game but first need to get my wheel collider and wheel mesh functioning properly but for some reason they don't appear to be in sync. The wheel colliders rotate for steering and for acceleration but the wheel mesh does not follow, instead it stays idle when testing.

This is the code I have used to get the vehicle moving and wheel colliders spinning which is supposedly meant to make the wheel mesh spin also.

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

[System.Serializable]
public class AxleCougarInfo {   
    public WheelCollider LeftWheel;
    public GameObject LeftWheelMesh;
    public WheelCollider RightWheel;
    public GameObject RightWheelMesh;
    public bool motor;
    public bool steering;
}

public class CougarController : MonoBehaviour {

    public List<AxleCougarInfo> axleInfos;
    public float maxMotorTorque;
    public float maxSteeringAngle;

    public void ApplyLocalPositionToVisual(AxleCougarInfo wheelPair)
    {
        wheelPair.LeftWheelMesh.transform.Rotate (Vector3.left, Time.deltaTime * wheelPair.LeftWheel.rpm * 10, Space.Self);
        wheelPair.RightWheelMesh.transform.Rotate (Vector3.right, Time.deltaTime * wheelPair.RightWheel.rpm * 10, Space.Self);
    }

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    public void FixedUpdate() {

        float motor = maxMotorTorque * Input.GetAxis ("Horizontal");
        float steering = maxSteeringAngle * Input.GetAxis ("Vertical");

        foreach (AxleCougarInfo axleInfo in axleInfos) 
        {
            if (axleInfo.steering == true) 
            {
                axleInfo.LeftWheel.steerAngle = steering;
                axleInfo.RightWheel.steerAngle = steering;
            }

            if (axleInfo.motor == true) 
            {
                axleInfo.LeftWheel.motorTorque = motor;
                axleInfo.RightWheel.motorTorque = motor;
            }
        }
    }
}
0

There are 0 best solutions below