Search Unity

Question Achieving multiple conflicting motions on a joint simultaneously

Discussion in 'Physics' started by Max-om, Dec 16, 2023.

  1. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    Hi!

    I'm trying to get object-holding physics working in a VR game I'm currently developing, but I have some trouble.

    I'm using a ConfigurableJoint that holds the object in the target location (where the hand is). Initially this created some problems with unsolvable constraints, such as moving the object into a wall. With high force settings, it causes absolutely horrible jittering, but with low force settings the object doesn't follow the movement of the hand adequately.

    That was solved by setting a low default force, but compensating by setting TargetVelocity to the hand movement delta each physics frame - so basically applying the hand-movement as TargetVelocity. Then, when the hand is in a wall, there won't be a huge force acting upon the object and causing jittering.

    However, this means that the anchor point for the joint must be very much in line with the transform for the hand, or applying the hand rotation will cause a large amount of drift (which will be slowly compensated for by the low default force - but it doesn't feel good).

    Now, the object moves smoothly and doesn't react badly to collisions. But in the case of a gun, I want to apply recoil. Since the rotation axes of the joint must necessarily be in the center of the gun / center of the hand, direct application of rotation via TargetVelocity doesn't work - the gun tilts upwards, sure, but it doesn't look natural since recoil (if rotational) must be applied around the wrist.

    I've tried simulating this by applying a small rotation around the x axis and simultaneously moving the gun backwards and upwards, but it's hard to get it to look good.

    Anyone have any suggestions on other methods to try or maybe a solution to this problem?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    I would animate recoil, not depend on the physics engine. Every gun is different and applies force to your hand differently.
     
  3. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    Animate it is not possible since this is a physics driven VR game and the recoil should effect the world as everything else. If I only animate the visual aspect of it the firearm will not affect other items and will clip both static and dynamic objects.

    I was hoping that maybe unity physics dev could give us some light how we could achieve this. Thanks.
     
  4. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    868
    Could you not just add a force in the correct direction to the wrist rigidbody to simulate recoil?
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    Look, making video games requires a certain amount of "verisimilitude." Looking realistic without actually modeling every little detail which will swamp the CPU or have major issues of control.

    When you hold a gun in real life, you keep your wrist quite relaxed... ready but relaxed. Otherwise your aim will be horrible. When you fire the gun, you anticipate the gun's movement and suddenly stiffen the wrist and elbow and shoulder significantly to catch the weapon as it kicks. If you model this realistically, you will need to model the changes to these joints in the physics system. Maybe you can take a few hours and fine-tune this system and it eventually works.

    But it's soooo much easier and still 99% as visually effective if you just animate the wrist override, and if you need to move other objects nearby, just add a spherical explosion force at the point of the gun's center of mass to kick away anything that's loose and within inches of it. Like, how often does that situation occur, where there's something loosely floating around and within inches of the gun? And let's say that's happening constantly because your game is literally "shooting things while standing in a cloud of loose floating objects." Will players dog you on their Steam reviews because the gun occasionally clips through a loose falling object?

    Developing a game is picking the battles against the complexity of reality that you can win.
     
  6. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    The thing is, I'd like physical recoil suppression to be a natural part of the game. Check the video below for the previous physics implementation (that we can't use since it had other limitations). At around 0:24 you can see the recoil being suppressed by pressing down on the barrel and keeping it steady against the metal surface underneath. So it's not just about floating clouds of dynamic objects, but an very natural implementation of stuff like bracing a machinegun.

    But otherwise, I'm fully on board with faking or skipping stuff that doesn't contribute to gameplay!

     
    Last edited: Dec 18, 2023
  7. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    @yant Maybe you can give us some insight in above problems? Thanks