Search Unity

Target Rotation doesn't match the target.

Discussion in 'Physics' started by holistic1, Sep 6, 2018.

  1. holistic1

    holistic1

    Joined:
    Jul 29, 2018
    Posts:
    8
    Hello, i am trying to make active ragdolls that mimics a certain static animated character. I am trying to achive this by Configurable Joint's Target Rotation system which uses springs to reach the target. Here's the script:


    Code (CSharp):
    1. public Transform[] targets;
    2.     public Rigidbody[] rigidbodies;
    3.     public Quaternion[] quaternions;
    4.  
    5.     void Start ()
    6.     {
    7.         quaternions = new Quaternion[11];
    8.         rigidbodies = GetComponentsInChildren<Rigidbody>();
    9.         targets = GameObject.Find("Static").GetComponentsInChildren<Transform>();
    10.     }
    11.    
    12.     void Update ()
    13.     {
    14.         for (int i = 0; i < rigidbodies.Length; i++)
    15.         {
    16.             if (i == 0)
    17.                 continue;
    18.            
    19.             rigidbodies[i].GetComponent<ConfigurableJoint>().targetRotation = targets[i+1].localRotation;
    20.         }
    21.     }
    This simply doesn't work. After i start each bodypart goes somewhere unexpected. Before anyone asks, there is no problem with matching the targets since during run-time as i play with the rotation of a specific body part, i get a result from the body part that i am expecting(Even though it's not the right result).I am not very familiar with the quaternions so maybe there is some aspect of these mathematical structures that i am missing.
     
  2. holistic1

    holistic1

    Joined:
    Jul 29, 2018
    Posts:
    8
    I was able to get over this problem by using mstevenson's script in GitHub which i am greatful for if he is reading this. Now i just need to understand how it works!
     
  3. ffsilvasurfa

    ffsilvasurfa

    Joined:
    Nov 16, 2019
    Posts:
    2
    this is because target rotation finds its rotation relative to what it started off with as a rotation. so if the objects have a rotation of 0,10,0. the mimic will go to 0,20,0 because it adds the targets position onto its own. one way to fix this is to set all the rotations of your player but this is not good as its weird to edit. the better way is finding start rotation of joint and then multiplying rotation by this. here is an example script
    {
    public GameObject target;
    public ConfigurableJoint j;
    JointDrive joint;
    Quaternion startPos;
    Quaternion startPoss;
    public GameObject attachedBone;
    public bool invert = true;
    private void Start()
    {
    startPos = Quaternion.Inverse(attachedBone.transform.localRotation);
    startPoss = attachedBone.transform.localRotation;
    }
    private void FixedUpdate()
    {

    joint.positionSpring = 10000;
    joint.maximumForce = 3.402823e+38f;
    joint.positionDamper = 0.05f;
    j.xMotion = ConfigurableJointMotion.Locked;
    j.yMotion = ConfigurableJointMotion.Locked;
    j.zMotion = ConfigurableJointMotion.Locked;

    j.angularXDrive = joint;
    j.angularYZDrive = joint;
    j.enablePreprocessing = false;
    if (invert)
    {
    j.targetRotation = target.transform.localRotation * startPos;
    }
    else
    {
    j.targetRotation = target.transform.localRotation * startPos;
    }

    }
    }