Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Execute script before initialization of physics

Discussion in 'Physics' started by Qleenie, Jun 25, 2020.

  1. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    851
    Hi,

    I have a simple problem for which I cannot figure any solution out:
    I want to scale a physics controlled character. However, if I just scale the character, all rigidbodies and joints seem to not scale with the scale of the parent transform.
    So my next idea was to scale on startup, in Start() or Awake(); however, it seems that at this point all physics is already initialized, with the same effect then scaling during runtime.

    Is there any solution to this problem (besides analyzing all joints and rigidbodies and move them accordingly)?

    Thanks for help!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    I don't think it's possible to run code before initializing the physics. A possible solution is creating the joints from code. Instead of using joint components directly, use a script that creates the joints in runtime in OnEnable (and destroys them in OnDisable). This way you should be able to change the scale anytime and have the character adapt to it by disabling and re-enabling it.
     
  3. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    851
    Hi Edy, thanks for help. This could be an option, however would require some significant work, as my characters consist of many joints, and however I feel a bit bad about putting data into code (in this case all joints configurations, which are a few dozens for sure). Probably I would then also need to do same with rigidbodies.
    There must be a way to reproduce what the editor does if you insert a joint (probably the auto configuration of connected anchor).
     
    Edy likes this.
  4. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    851
    I guess I just somehow answered my own question:
    Code (CSharp):
    1.         foreach (Joint joint in transformToSize.GetComponentsInChildren<Joint>())
    2.         {
    3.             if (joint.autoConfigureConnectedAnchor)
    4.             {
    5.                 joint.autoConfigureConnectedAnchor = false;
    6.                 joint.autoConfigureConnectedAnchor = true;
    7.             }
    8.         }
    This seems to re-initialize the joints.
    Thanks for help, this brought me on the right track.
     
    Edy likes this.