0
\$\begingroup\$

I'm fairly new to Unity and game development in general, I'm trying to build a basic car and give visual movement to the wheels. The car controls are pretty basic: accelerate "w" key, turn right "d" key, turn left "a" key, decelerate "s" key. The wheel moves and turns according to the key pressed, but the problem is that the wheel changes its position during the game mode. I am only working with one wheel until I know how to do this correctly.

The player object has a Rigidbody component and the movement script, also is composed by two childs objects: the car body, this object has a mesh collider component, and the wheels, this object also has their own child objects: wheel colliders and wheel models.

This is what the wheels look like from edit mode:

wheels_from_edit_mode

And this is how the wheels look like from play mode:

wheels_from_game_mode

Code from the script:

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

public class SimpleCarController : MonoBehaviour
{
   [SerializeField] private Rigidbody _rb;

   [SerializeField] Transform frontLeftTransform;
   [SerializeField] Transform backLeftTransform;

   [SerializeField] WheelCollider frontLeft;
   [SerializeField] WheelCollider frontRight;
   [SerializeField] WheelCollider backLeft;
   [SerializeField] WheelCollider backRight;

   public float acceleration = 500f;

   public float breakingForce = 300f;

   public float maxTurnAngle = 50f;

   private float currentAcceleration = 0f;
   private float currentBreakForce = 0f;
   private float currentTurnAngle = 0f;

   void Start()
   {
       _rb = GetComponent<Rigidbody>();
   }

   public void FixedUpdate()
   {
       currentAcceleration = acceleration * -Input.GetAxis("Vertical");

       backRight.motorTorque = currentAcceleration;
       backLeft.motorTorque = currentAcceleration;

       if (Input.GetKey(KeyCode.Space))
           currentBreakForce = breakingForce;
       else
           currentBreakForce = 0;

       frontRight.brakeTorque = currentBreakForce;
       frontLeft.brakeTorque = currentBreakForce;
       backLeft.brakeTorque = currentBreakForce;
       backRight.brakeTorque = currentBreakForce;

       currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal");

       frontLeft.steerAngle = currentTurnAngle;
       frontRight.steerAngle = currentTurnAngle;

       UpdateWheel(frontLeft, frontLeftTransform);
       UpdateWheel(backLeft, backLeftTransform);
   }

   void UpdateWheel(WheelCollider col, Transform transform)
   {
       Vector3 pos;
       Quaternion rot;

       col.GetWorldPose(out pos, out rot);

       transform.position = pos;
       transform.rotation = rot;
   }
}

I don' know why this, any help is appreciated.

--UPDATE-- Image with gizmo in Local/Pivot:

gizmo_local_pivot

\$\endgroup\$
7
  • \$\begingroup\$ Can you show us a screenshot with one wheel mesh selected, with the transform gizmo in Local/Pivot mode (toggles in the top-left of the Scene View window), so we can see where the local origin of the wheel mesh is? \$\endgroup\$
    – DMGregory
    Commented Apr 9, 2023 at 12:26
  • \$\begingroup\$ @DMGregory I have updated the question adding the image with the gizmo in Local/Pivot \$\endgroup\$
    – Daniel
    Commented Apr 9, 2023 at 20:34
  • \$\begingroup\$ That'll do it. The position you're reading from your wheel collider is the center of your physical wheel. The position you're applying it to is the bottom of the visible wheel. Setting the bottom of the visible wheel to be equal to the center of the physical wheel makes the visible wheels levitate one wheel radius too high. \$\endgroup\$
    – DMGregory
    Commented Apr 9, 2023 at 20:42
  • \$\begingroup\$ @DMGregory ok, I understand, but is there any way to modify the position of the pivot in Unity? The wheels are an imported object from Blender, the pivot in Blender is in the center of the wheel but not in Unity. Also, I have tried to create some wheels from a cylindrical shape in Unity but the same thing happens. \$\endgroup\$
    – Daniel
    Commented Apr 9, 2023 at 22:42
  • \$\begingroup\$ Did you search? \$\endgroup\$
    – DMGregory
    Commented Apr 9, 2023 at 22:48

1 Answer 1

1
\$\begingroup\$

The problem here is that the wheel pivot positions are at the bottom of the wheel models. I fixed this by adding an empty game object, changing the position where it needed to be and then added the wheel model as a child of the empty game object that has the pivot right in the center, something like this:

wheels_fixed

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .