Search Unity

How to copy inertia from 2D to 3D

Discussion in 'Physics' started by RedHotFlashman, Feb 28, 2020.

  1. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    35
    In a side scroll racing game like "Hill Climb Racing", I want to temporally use 3D physics to make a turn and then switch back to 2D physics. When I make the switch from 2D to 3D, the car acts as if the brakes are pressed for a split second. After some experiments I came to realize this is an inertia problem, But how do I set the inertia on the rigidbody of a vehicle3D?

    This is the code I have so far. But the inertia part is not correct at all.
    When I log the inertiaTensorRotation and inertiaTensor from the vehicle3D.rigidBody, I always get the same values, right from the start; and when moving.
    So how do I correctly kick-start a vehicle3D?

    public void CopyVehicle2DtoVehicle3D() {

    float rot2D = -vehicle2D.rigidBody.transform.eulerAngles.z;
    if (rot2D < 0) rot2D += 360;
    vehicle3D.rigidBody.transform.rotation = Quaternion.Euler( rot2D, 90, 0 );

    vehicle3D.rigidBody.transform.position = vehicle2D.rigidBody.transform.GetWorldPosition();

    vehicle3D.rigidBody.velocity = vehicle2D.rigidBody.velocity;

    float inertia2D = vehicle2D.chassisRigidB.inertia;
    rigidBody.inertiaTensorRotation = Quaternion.Euler( inertia2D, 0, 0 );
    }