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

DrawMeshInstanced invalid equation

Discussion in 'Scripting' started by alexandersyurchenko, May 12, 2020.

  1. alexandersyurchenko

    alexandersyurchenko

    Joined:
    Oct 29, 2019
    Posts:
    9
    When passing Quaternions to the Graphics.DrawMeshInstanced I'm getting this error:

    Quaternion To Matrix conversion failed because input Quaternion is invalid {0.000000, 0.670043, 0.000000, 0.742312} l=0.999985

    From this line:

    matrix.SetTRS(pos.Value + new float3(0, 0.01f, 0), rot.Value, Vector3.one);


    I get that error because of this operation:

    rot.Value = math.slerp(rot.Value, quaternion.LookRotationSafe(direction, math.up()), turnSpeed.Value * deltaTime);


    If I don't slerp the rotation value I don't get that error.

    Could someone help me understand why that causes an issue and what might be a good solution, please?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    That conversion error sounds to me like maybe the Quaternion didn't come out of Slerp properly normalized, which seems unlikely, but I base this on the
    l=0.999985
    snippet.

    Try calling
    .Normalize()
    on the Quaternion after you get it out of Slerp but before you pass it to SetTRS... I would be curious if that fixes it. Perhaps someone with more math background can chime in here?
     
  3. alexandersyurchenko

    alexandersyurchenko

    Joined:
    Oct 29, 2019
    Posts:
    9
    Thanks Kurt-Dekker! I've change the slerp code to


    rot.Value = Quaternion.Normalize(math.slerp(rot.Value, quaternion.LookRotationSafe(direction, math.up()), turnSpeed.Value * deltaTime));



    But now I get these errors. I've looked it up but I'm not quite sure what it means.
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Check the docs for Quaternion.Normalize():

    https://docs.unity3d.com/ScriptReference/Quaternion.Normalize.html

    I quote,

    "Note that this method will change the current quaternion. If you want to keep the current quaternion unchanged, use the normalized property instead."

    You are normalizing the transient value, what slerp returns, which I don't even know what that would do... Assign that instead to a temporary variable and see if it works any better.

    In general it's better to keep your code lines shorter, just do things one step at a time, not all tangled up into one giant line.