- Home /
Quaternion to Matrix Conversion Error - Camera Script
I've made an alteration to the smooth follow script and I'm running into some Quaternion errors after making the changes. I've figured out that if I remove one variable (the time variable from the forward transform) from the script the error goes away but then the rotation is very choppy. Thus I need to multiply it by the time variable to keep a smooth rotation.
Here is the main section of the script:
void LateUpdate() {
if (!isStarting) {
// Calculate the current rotation angles
float wantedRotationAngle = player.transform.eulerAngles.y;
float currentRotationAngle = transform.eulerAngles.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = player.transform.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.forward += player.transform.forward * rotationDamping * Time.fixedDeltaTime;
// Set the height of the camera
Vector3 currentPosition = this.gameObject.transform.position + new Vector3 (0, height, 0);
this.gameObject.transform.position = currentPosition;
}
else
{
transform.position = Vector3.Lerp(transform.position, player.transform.position + new Vector3(0, 7, 0), 2.0f * Time.deltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation, player.transform.rotation, 2.0f * Time.deltaTime);
}
}
The Quaternion to Matrix Conversion error is as follows: Quaternion To Matrix conversion failed because input Quaternion is invalid {-0.591886, -0.335223, -0.285926, 0.676890} l=1.002638
The error is coming from the line: transform.forward += player.transform.forward rotationDamping Time.fixedDeltaTime;
If I remoeve the Time.fixedDeltaTime I don't get the error anymore but then the rotation is very choppy and looks awful.
Any help out there? Thanks!
Why are you using fixedDeltaTime
inside LateUpdate
? fixedDeltaTime
is the time between calls to FixedUpdate
. Also, by setting forward
directly you are effectively setting the rotation matrix component of the Transform. You probably need to make sure that your change to forward doesn't invalidate right and up.
I forgot to change it back before posting this. I was using regular deltaTime. I was just trying fixed to see if that could be the issue. If I used Time.time ins$$anonymous$$d of deltaTime the error goes away though. But again the rotation is not smooth. It's slightly odd. I had a feeling that setting forward directly might be the issue, but as soon as I removed the frame rate dependence then it doesn't work... so I thought hey maybe someone can see what's going on here. At this point I might just try and write something else, ins$$anonymous$$d of altering the smooth follow script. Too bad, because if this error could go away the script does exactly what I want :) Thank you for your help though.
Well, as I said, because you are changing the forward vector only, the 3x3 matrix that is the rotation component of the transform is no longer orthogonal. Think of it this way, forward, right and up form the corners of a cube. You've changed the direction that forward points in. If this was a cube, then right would change also. You can think of this as the cube being rotated around the up vector. In your case, only forward has changed, and right still points whereever it used to. This means the cube is now sheared and these three vectors are no longer at ninety degrees to each other. Thus your rotation matrix is no longer orthogonal. Unity has tried to use a quaternion during the maths, and concluded that the matrix is impossible to create. (In quaternion terms, the quaternion should have a unit length, but it doesn't. I guess whether Unity spots the problem or not depends on how much your forward vector deviates, which leads to the quaternion changing length.) I still think you'd be better off computing the angle you want forward to rotate through, and then rotate the axes as a whole.