Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

My character occasionally flips upside down. help!

Discussion in 'Scripting' started by Nanako, Mar 11, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Throughout my current project i've had a chronic problem. My character would, occasionally, simply flip on his head, and stay that way forever. while still moving and walking around normally.

    I've never been able to pin down an exact repro for this problem, however it seemed to mostly occur when the character tries to rapidly turn 180, and/or just after the character walks off a ledge.

    It's worth noting here that my character is physical. He has a rigidbody and a collider, and i move him by using AddForce while movement keys are held down. I despise kinematic movement. I'm not sure if this related to my issues.

    Up until today, i've been setting my character's rotation by using MoveRotation to rotate towards the keyboard input, relative to the camera (EG, pressing W would always point him immediately in the same direction as the camera)

    I calculated this by using Quaternion.FromToRotation, with my character's forward vector, and the camera's forward vector (with the Y component of both set to 0)

    Anyways today, for unrelated reasons, i've moved to a simpler (and yet more complex) method of rotating my character, inspired by overgrowth. Basiucally i now use MoveRotation to point him in the direction of his velocity, so he turns more slowly when he's changing speed slowly, but he still turns rapidly if i try to go the opposite way he's facing from a standstill. The code i'm using for this now is:

    Code (CSharp):
    1.         body.MoveRotation(Quaternion.FromToRotation(transform.forward, VectorTools.Flatten(body.velocity)) * body.transform.rotation);
    Body is a reference to the rigidbody, Flatten is a simple function which just returns (X, 0, Z)

    That line is now the only thing affecting his rotation, and there's no dependancy on the camera anymore. The flipping upside down problem is now mostly eliminated, and yet in some cases it has gotten worse. Most notably, i now have a reliable repro for it:

    When i enter play mode, he starts facing the camera. Pressing W gives him some velocity away from the camera, and instantly causes him to flip and fall through the world.



    I can also occasionally cause him to flip by aligning the view just right and hitting W.
    The pattern here is obvious, he flips upside down whenever he tries to turn 180 degrees in an instant. I'm guessing it's because, at that perfect point, all paths to the other side are of equal length, and there's no particular reason for him to rotate around his Y axis

    The question is, what can i do about that?

    would it help to upload my project for someone to inspect?
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    What sort of colliders does he have attached?
     
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    just one

     
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    so this problem is still bugging me ;-;