Search Unity

Rotating Gameobjects with 2D Hinges.

Discussion in 'Physics' started by KNR, Jul 19, 2015.

  1. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    Hey

    I have this problem that i have been working with quite a while now and i havent figured really a good way to deal with it.
    Basically i have a 2D bike which has all its componets attached to each other with 2D hinges and rigidbodies.
    The bike gameobject consists of bike holder gameobject where all the individual bike parts and driver parts are child gameobjects.
    BikerPrefab.PNG

    Im having problem of basically rotating the direction of the bike 180 degrees on y axis.
    In practise i want the player to be able to change the direction of the bike with a press of a button.
    bikerPrefab2.PNG
    As it is at the moment i have tried 3 different methods of rotating the bike gameobject
    Rotating the parent bikeholder objects y axis with quaternions.euler.
    Rotating individual parts of the bike/driver's y axis with quaternions.euler.
    Multiplying all individual bike/driver part's localscale x values by -1.

    The localscale change seems to work somewhat. i have gotten it to work in a really hacky way that i change the quaternion rotations depending on certain angle. Otherwise it does this strange behaviour what you can
    inspect on the web scene that i have link below.

    The problem becomes clear and visual when the bike reaches 360 degrees of rotation especially when you invert the x axis localscale.

    Im not sure what causes the sudden 360 rotations on the suspensionbar but i do have a feeling it has something do do with the limit function of hinges,It might be the limit that acts odd when certaing angle is reached.

    I made small scene where you can better see what the problem is and hopefully tell me what is causing the strange behaviour and how to fix it.

    Link to Webscene.
    http://koti.tamk.fi/~e4tkaner/Unity/HingeBehaviour/HingeBehaviour.html

    Sincerily Knr
     
  2. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    I have figured a out a hacky way to deal with this problem. I think the problem has something to do with the hinge joint limits and quaternion angles of the gameobjects.

    For example the bike object in the picture that i have uploaded has its chassis at 0 degree angle in the inspector and the rear suspension slightly positive value.
    When you rotate the character the quaternion z & w values change between from positive 1 to negative 1.
    Everytime the chassis changes its value from positive to negative and viceversa the suspension bar is still in a different quaternion angle.

    This is the exact moment when the unwanted rotation happens with gameobjects with limits set on, it actually happens everytime the bike rotates over the 360 radius or under 0 in the inspector panel, this is also the time quaternion values change from positive to negative.

    Way i got the player model working without any unwanted rotations were multiplying the quaternion z and w values by -1 every time the game object is closing in the rotation over 360 or below 0 mark in inspector. But as i understand changing the quaternion values is really a not good practise.

    As i tried to rotate the character without the limits set on, the unwanted rotation stops.
    It seems goodway to deal with this would be trying to script your own angle limits.

    Sincerily Knr