Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question how to tackle a curve on a motorcycle using wheel collider

Discussion in 'Physics' started by ArcaDone, Apr 10, 2021.

  1. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    To implement a movement that is as physical and natural as possible, I am using the Wheel colliders for the wheels of the bike. I am applying the reverse pendulum rule to hold the bike up when it is stationary as I tilt the bike left and right by applying a force like this:

    Code (CSharp):
    1. void Power(Transform transformOBJ, int direction)
    2.     {
    3.         int layerMask = 1 << 8;
    4.  
    5.         layerMask = ~layerMask;
    6.         RaycastHit hit;
    7.  
    8.         if (Physics.Raycast(transformOBJ.position, Vector3.down, out hit, Mathf.Infinity, layerMask))
    9.         {
    10.             Debug.DrawRay(transformOBJ.position, transformOBJ.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
    11.             float proportionalHeight = (_height - hit.distance) / _height;
    12.  
    13.             Vector3 appliedHoverForce = Vector3.up * proportionalHeight * inclinazione;
    14.  
    15.             _rb.AddForceAtPosition(-1 * appliedHoverForce, transformOBJ.position, ForceMode.Acceleration);
    16.             _rb.AddTorque(direction * Vector3.one * 1, ForceMode.Impulse);
    17.  
    18.         }
    19.         else
    20.         {
    21.             Debug.DrawRay(transformOBJ.position, transformOBJ.TransformDirection(Vector3.down) * _height, Color.green);
    22.         }
    23.     }
    The bike rotates correctly at low speeds, while at high speeds the centrifugal force overcomes the centripetal force and the bike cannot go around the corner. the ones below are the parameters I use. I don't know if I have to change the sideways friction.

    Schermata 2021-04-10 alle 22.24.03.png