Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Add Torque from another objects current rotation?

Discussion in 'Physics' started by FuzzyOnion, Mar 2, 2020.

  1. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    Hi,

    I haven't been able to find a solution to this. I'm trying to add torque to a rigidbody in the same direction as leader object's current rotation.

    I think I'm calculating the direction of the leader object's rotation with a CurrentOrientation * Quaternion.Inverse(OldOrientation)

    I then AddTorque to the follower converting the result to a eulerAngle but the resulting rotation is not in the same direction.

    Any help would be much appreciated.
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Euler Angela are an agreed upon sequence of rotations, one after another. What you’re looking for is probably the Vector3 around which you can rotate the one object to match the other one.. I just went quickly over the quaternion docs and found this:
    https://docs.unity3d.com/ScriptReference/Quaternion.ToAngleAxis.html

    use that on the quaternion that represents the “difference” of rotation between your objects like you already have. Maybe you need to swap around the parts before and after the multiplication, but I guess you’re on a good path!

    let me know if it helps :)
     
  3. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    Thank you for the reply, I'm not quite sure how ToAnglesAxis relates to this problem. Here's how I'm trying to use it but I'm guessing that's incorrect:
    Code (CSharp):
    1.  void FixedUpdate()
    2.     {
    3.         LeaderObj.transform.LookAt(rt);
    4.  
    5.         CurrentRotation = LeaderObj.transform.rotation;
    6.         DirofRotation = LastRotation * Quaternion.Inverse(CurrentRotation);
    7.       //  var boop = Quaternion.Inverse(DirofRotation);
    8.        var RotAngleX = Quaternion.AngleAxis(torque, DirofRotation.eulerAngles);
    9.         rb.AddTorque(RotAngleX.eulerAngles * torque , myForce);
    10.         Debug.Log(DirofRotation);
    11.         LastRotation = LeaderObj.transform.rotation;
    12.     }
     
  4. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    WOot!
    I found a solution here.

    The solution:
    Code (CSharp):
    1.  void FixedUpdate()
    2.     {
    3.         LeaderObj.transform.LookAt(rt);
    4.  
    5.         CurrentRotation = LeaderObj.transform.rotation;
    6.         DirofRotation = CurrentRotation * Quaternion.Inverse(LastRotation);
    7.         rb.AddTorque(DirofRotation.x * torque, DirofRotation.y * torque, DirofRotation.z * torque, myForce);
    8.         Debug.Log(DirofRotation);
    9.         LastRotation = LeaderObj.transform.rotation;
    10.     }