[Solved] Car cannot moving

A simple car was created with Unity.

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

public class BackHoeController : 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 * Quaternion.Euler (0f, 0f, 90f);
    }

    void Start () {
        Debug.Log ("CarStart");
    }
   
    void Update () {
       
    }

    public void FixedUpdate()
    {
        float motor = maxMotorTorque * Input.GetAxis("Vertical");
        float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
        Debug.Log ("CarFUpdate motor=" + motor + "steering=" + steering);

        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;
    public bool steering;
}

However, I am having trouble with it, because when I operate it right after it starts, it only moves a little, and when I operate it some time after it starts, it doesn’t move at all.
Please let me know what information you need and I will post it.

Thank you.

Your code is copy-pasted from the tutorial Unity - Manual: WheelCollider Tutorial , which is fine, but then also make your object hierarchy according to the tutorial. You need to match the setup given in the tutorial. And by the way, your right wheels are a child object of your left wheels.

Did you check the “AxleInfo”'s motor and steer in your good locations?
I have a simple tutorial video.

Sorry for the much delayed reply.

I am writing to you from this site (https://digilab.tech/【unity】自作3d自動車を作�% 88%90%E3%81%99%E3%82%8B/) and created it but apparently this site refers to the site you presented.
Thanks to you, I followed that tutorial and it worked fine. Thank you very much.

1 Like

Like #2, sorry for the much delayed reply.

In reply to #2, I got it to work for now, but I have only actually run the tutorial and have not caught up with my understanding. I am not good at English and could not find these videos. I think I need to watch these videos to gain more knowledge and fully understand why it didn’t work. Thank you for the valuable information.

1 Like