Search Unity

2 separate operations on a Config Joint / Rigidbody within a single FixedUpdate

Discussion in 'Physics' started by Fu11English, Apr 6, 2018.

  1. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Problem - It seems these two 'operations' cannot be performed in succession. All I get is the second part (AddTorque). My guess is Unity combines the entire thing and runs it together at the end of the FixedUpdate. Can I overcome this somehow or is that just how it works?

    Code (CSharp):
    1.  private void FixedUpdate()
    2.     {
    3.         JointDrive jd1 = new JointDrive
    4.         {
    5.             positionSpring = Mathf.Infinity,
    6.             maximumForce = Mathf.Infinity,
    7.             positionDamper = 50f
    8.         };
    9.         bike_CJ.slerpDrive = jd1;
    10.  
    11.         bike_CJ.xMotion = ConfigurableJointMotion.Locked;
    12.         bike_CJ.yMotion = ConfigurableJointMotion.Locked;
    13.         bike_CJ.zMotion = ConfigurableJointMotion.Locked;
    14.         bike_CJ.targetRotation = Quaternion.AngleAxis(30 * lean, bike_TR.forward); // lock the joint, rotate it with target rotation force
    15.  
    16.         JointDrive jd2 = new JointDrive
    17.         {
    18.             positionSpring = 0f,
    19.             maximumForce = 0f,
    20.             positionDamper = 0f
    21.         };
    22.         bike_CJ.slerpDrive = jd2;
    23.  
    24.         bike_CJ.xMotion = ConfigurableJointMotion.Free;
    25.         bike_CJ.yMotion = ConfigurableJointMotion.Free;
    26.         bike_CJ.zMotion = ConfigurableJointMotion.Free;
    27.         bike_RB.AddTorque(Vector3.up * 10, ForceMode.Impulse); // disable target rotation force, unlock the joint, rotate it with AddTorque
    28.     }
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    That's how it works. Note that your code is not performing any operation in the joint: it's just configuring the public properties exposed by the joint component. At the end of FixedUpdate the physics engine takes the values that are in the properties and performs the physic operations with them. So physics only "sees" the latest values you left there.
     
  3. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Yeah I figured as much and was thinking about things all wrong. Cheers.