Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Help Wanted How is rotation constraint calculated?

Discussion in 'Animation' started by Xriuk, Jan 10, 2021.

  1. Xriuk


    Aug 10, 2018
    I need to update manually the rotation constraint component, calculate it (default component, not animation rigging one).

    I have already updated/calculated the position constraint and the order of the sources doesn't matter. But apparently in the rotation constraint it does.
    I've tried calculating it by "averaging" the rotation of each source in order, but I think I might be getting something wrong here...

    Code (CSharp):
    1. Quaternion quat = Quaternion.identity;
    2. float weights = 0f;
    3. for(int i = 0; i < constraint.sourceCount; i++) {
    4.     weights += constraint.GetSource(i).weight;
    5. }
    6. for(int i = 0; i < constraint.sourceCount; i++) {
    7.    quat *= Quaternion.Slerp(Quaternion.identity, constraint.GetSource(i).sourceTransform.rotation, constraint.GetSource(i).weight / weights);
    8. }
    With this code I expect to get the weighted average world rotation of the sources of the constraint, but it's not working...
    Last edited: Jan 10, 2021
  2. Xriuk


    Aug 10, 2018
    My doubt comes from a test that I've made with 3 sources with the same weight and these rotations:
    • A (0, 30, 0)
    • B (0, -30, 0)
    • C (0, 0, 0)
    If I change the positions I get different results everytime in the Unity implementation, why is that? C shouldn't affect the rotation, but it does...