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. Dismiss Notice

Quaternion To Matrix conversion failed because input Quaternion is invalid?

Discussion in 'Scripting' started by squared55, Feb 5, 2015.

  1. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I'm getting this error and I have no idea why. It doesn't give a script line or anything. I *think* this is the line that's causing it, but I'm not sure:


    EDIT: Turns out it's not the line. The problem appears to be occurring when I set the rotation in LateUpdate to a bone which is also being animated by mecanim. It only occurs in Late Update, but I can't move it because I need to override the animation.

    Code (JavaScript):
    1. s.rotation = Quaternion.FromToRotation(b.forward, t.position - b.position);
    Any ideas on how to resolve this?
     
    Last edited: Feb 5, 2015
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Hmm..the only thing that I can see from the Unity manual that looks somewhat different than
    your calls is that they are using normalized vectors for both From and To.
    So maybe try normalizing "t.position- b.position" and see ? This is
    just a quick guess.
     
    squared55 likes this.
  3. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Unfortunately, that didn't work.

    EDIT: I tried commenting the whole line out... and the error appeared anyways. Great.
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    aah ok, in that case, I'd first locate for sure where that error is coming from.
     
  5. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I'd love to but it only appears a few times a playthrough at no apparent time. I'd love to know exactly what causes the error,

    Weirdly enough, if I use the print method right after it, it doesn't happen. Maybe if I move it to fixed update or something it'll work.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    My guess is that "t.position - b.position", or some equivalent portion of code elsewhere, creates a zero-length vector. That would be pretty rare, since both positions would have to be equal, so that explains why you rarely see it.
     
  7. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I tried just plugging in Vector3.zero into the equation, but it didn't spam the console with warnings, so I don't think that's the problem. The issue seems to occur only when you apply the quaternion to an actual object. It doesn't seem to effect gameplay at all, the only issue is that there is an error. If there any way to suppress it?

    Here's the full error:

    "Quaternion To Matrix conversion failed because input Quaternion is invalid {0.407859, 0.719009, 0.346307, 0.435520} l=0.992929"

    I know nothing of the internal workings of quaternions, so I have no idea what that means.

    The error only appears if I set the rotation in LateUpdate. However, I can't move it as I need it to override a Mecanim animation.
     
    Last edited: Feb 5, 2015
  8. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I could be wrong, but I think I fixed it by changing another script that rotated another object higher up in the hierarchy to do so in LateUpdate rather than FixedUpdate. Crossing my fingers and hoping the issue doesn't pop up again.

    It's hard to tell because there isn't any set way to test it besides hitting play and waiting.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    On further thought, it would actually make perfect sense to me if that just returned you no rotation. Oops!

    I think the bolded bit might be telling you that it's not a normalized quaternion.
     
    squared55 likes this.
  10. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Yep, this is what I remarked in my reply earlier..it looks like it needs to be normalized
     
    squared55 likes this.
  11. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Well, in the two inputs into Quaternion.FromToRotation are normalised, then the quaternion should be valid. I wonder if the code we see isn't the code that produces this problem?
     
    squared55 likes this.
  12. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Well, I did end up fixing it. The intent of this script was to rotate an object so it's child points at a target. Simply calculating the new rotation never caused the error; the problem occurred when I applied it to the object's transform. It was in LateUpdate so it would override mecanim.

    However, the object I was rotating also had a parent, which was also being rotated, and this was done during FixedUpdate (I was under the impression this would improve performance). The error only occured when the scripts were combined. By rotating that parent in LateUpdate as well, the error vanished.

    The log was also never spammed with errors. It would only come up maybe 1 or 2 times during each run-through of the game, and it never caused any problems besides being a minor annoyance.
     
    FireAshen likes this.