Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[Help] Implementing Rotation Multiplicator

Discussion in 'Scripting' started by iSinner, Jul 16, 2019.

  1. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    154
    Lets say i have a certain rotation on object A, i want to take his rotation and double it for example, and assign it to object B.

    I tried to do it like follows:
    Code (CSharp):
    1.  
    2. var rot = this.transform.localRotation;//Object A
    3. var resultRotation = Quaternion.SlerpUnclamped(Quaternion.identity, rot, Multiplier);
    4. Affected.localRotation = resultRotation;//object B
    5.  
    Which has some weird issues with values of Multiplier being anything but 1.
    For example, with the Multiplier=2, if i rotate around Y axis for 15 degrees, then around X axis, the rotation does not occur around the X axis of the object B, it occurs around the X axis of object A. And i can not understand why. Is the implementation wrong, or is there something else going on?

    Does someone know how a multiplier to a rotation could be implemented in such a way that in the scenario given above, it would behave as expected? like if i rotate around Y with 15 degrees the object A, then it would rotate the object B around Y by 30 degrees, and if i afterwards rotate around X axis, it would rotate around the X axis of object B and not object A.

    This is the script i am using, it is located on the source object from which the rotation will be taken, and it applies the rotation to the "Affected" transform.

    Code (CSharp):
    1. public class RotationMultiplicator : MonoBehaviour
    2. {
    3.     public Transform Affected;
    4.  
    5.     public float Multiplier = 1;
    6.  
    7.     public void Update()
    8.     {
    9.         var rot = this.transform.localRotation;
    10.         var resultRotation = Quaternion.SlerpUnclamped(Quaternion.identity, rot, Multiplier);
    11.         Affected.localRotation = resultRotation;
    12.     }
    13. }
     
    Last edited: Jul 16, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
    I can't see where do you multiply rotations? Slerp is just linear interpolation, it gives you average value between a and b based on weight (why do you call it multiplier, by the way?). The call
    Code (CSharp):
    1. Quaternion.SlerpUnclamped(Quaternion.identity, rot, Multiplier)
    will return Quaternion.identity for weight = 0, rot for weight = 1, and for values outside 0-1 it will return rotations to finish the circle defined by those two points on the unit sphere.
     
  3. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    154
    Since the slerp is unclamped, it gives me(or so I think) double rotation for the Multiplier value of 2. That's why I call it multiplier.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
    No, you a wrong. In slerp, how much is one depend on the length of the arc defined with two rotations. For instance, lerp(10, 12, 1) = 12
    lerp(10, 12, 0) = 10
    lerp(10, 20, 2) = 14
     
  5. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    62
    no, LerpUnclamped(10,20,2) = 30.
    Lerp(10,20,2) = 20
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
    It should be lerp(10,12,2) as two previous, just a typo. lerp(10,20,2)=30 do not illustrate the problem well enough
     
  7. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    154
    I am not sure how i am wrong because this is how i understand it. And the implementation is build around this understanding, so what exactly is wrong?

    From the code i posted, i do a slepr from identity to object's A rotation with t=2(which is the value of Multiplier), which should give me double rotation of A, correct?
    Code (CSharp):
    1. var rot = this.transform.localRotation;//Object A
    2. var resultRotation = Quaternion.SlerpUnclamped(Quaternion.identity, rot, Multiplier);
    3. Affected.localRotation = resultRotation;//object B
    Lets say you have a zero rotation quaternion, that is identity, we will label it A.
    And some random rotation from the identity, which we will call B.
    Wouldn't slerp from A to B with t=2 be a double rotation of B from identity, since A is identity? hence the double rotation?

    From the example you gave, that is exactly what i expect it to do.

    I do a slepr(0,10,2) and i expect 20.
     
    Last edited: Jul 16, 2019
unityunity