Search Unity

Physics based characters?

Discussion in 'Physics' started by JMgames, Dec 13, 2014.

  1. JMgames

    JMgames

    Joined:
    Sep 21, 2014
    Posts:
    4
    In gta 5, if you walk off a cliff your body tumbles down even while your alive, and the movements are driven by physics and animation, how would this be achieved in unity?

    Also more features such as getting shot being able to knock you over, large objects knocking you over, etc.

    I want to use advanced physics driven movement in my 3ps Fps game.

    Fun experiment: place the first person controller down, then rotate it forward. Try to move around.
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i think tumbling effects and such are done with ragdolls.

    As to doing physics properly in general, a good start is to have your character move around and jump using Applyforce, as well as giving them a rigidbody and a sensible mass for their size.

    would be interesting to see others' input on this. that's about as far as i've gotten for now
     
  3. JMgames

    JMgames

    Joined:
    Sep 21, 2014
    Posts:
    4
    What about the animators; "apply physics" could that be used for animating the player using physics?
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    With Unity, ConfigurableJoints can have a target orientation and apply forces to reach that position.

    For pose matching, you'd basically have a hidden version of your character, powered by normal animation, and a physical version of your character where each joint is trying to match the orientation of the animated version.

    We did this in Minotaur China Shop, although the game was deliberately meant to be clumsy. You're going to have a seriously difficult time accomplishing precise/FPS movement here.

    Here's a 6-year old video (!):



    And the actual code on each joint (UnityScript):

    Code (csharp):
    1.  
    2. #pragma strict
    3. @script AddComponentMenu("") // hidden to users
    4. @script RequireComponent(ConfigurableJoint)
    5.  
    6. // the object we're trying to match
    7. var target:Transform;
    8.  
    9. // our individual strength
    10. var strength:float = 1.0;
    11.  
    12. // our starting angle
    13. private var initialRotation:Quaternion;
    14.  
    15. // our initial strengths
    16. private var initialSpring:float;
    17. private var initialDamper:float;
    18.  
    19. // cache
    20. private var transformC:Transform;
    21. private var jointC:ConfigurableJoint;
    22.  
    23. /**
    24. * Set up our initial values
    25. */
    26. function Awake()
    27. {
    28.     transformC = transform;
    29.     initialRotation = transformC.localRotation;
    30.  
    31.     jointC = GetComponent(ConfigurableJoint);
    32.  
    33.     initialSpring = jointC.slerpDrive.positionSpring;
    34.     initialDamper = jointC.slerpDrive.positionDamper;
    35. }
    36.  
    37. /**
    38. * Change the joint angle target to match our target's
    39. */
    40. function FixedUpdate()
    41. {
    42.     if(!target)
    43.         return;
    44.  
    45.     jointC.targetRotation = Quaternion.Inverse(target.localRotation) * initialRotation;  
    46. }
    47.  
    48. /**
    49. * Modify our joint strength by a percentage
    50. */
    51. function SetStrength(newStrength:float)
    52. {
    53.     // modify by our individual strength
    54.     newStrength *= strength;
    55.  
    56.     // negatives will probably freak out the physics engine
    57.     if(newStrength < 0.0)
    58.         newStrength = 0.0;
    59.  
    60.     var positionDamper = initialDamper * newStrength;
    61.     var positionSpring = initialSpring * newStrength;
    62.  
    63.     jointC.slerpDrive.positionDamper = positionDamper;
    64.     jointC.slerpDrive.positionSpring = positionSpring;
    65.  
    66.     jointC.angularXDrive.positionDamper = positionDamper;
    67.     jointC.angularXDrive.positionSpring = positionSpring;
    68.  
    69.     jointC.angularYZDrive.positionDamper = positionDamper;
    70.     jointC.angularYZDrive.positionSpring = positionSpring;  
    71. }
    72.  
    Joint angles are defined relative to the starting position of the joint, not in the object's local frame relative to its parent. You'll need to do a little Quaternion/rotation math there to compensate, but really, that's the meat of it.
     
  5. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  6. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,684
    Minotaur China Shop! lol