- Home /
The question is answered, right answer was accepted
Quaternion To Matrix conversion failed because input Quaternion is invalid
I suddenly got this error. I don't know what went wrong. I can still play my game, but I get this error when I slide my character.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
private GameObject player;
PlayerController playerController;
public float cameraHeight = 4;
public float cameraDistance = 4;
public float smooth = 3;
private float newCameraHeight;
private int direction;
// Use this for initialization
void Start ()
{
player = GameObject.Find ("Player");
playerController = player.GetComponent<PlayerController>();
transform.position = new Vector3 (player.transform.position.x, cameraHeight, player.transform.position.z);
newCameraHeight = cameraHeight;
}
// Update is called once per frame
void Update ()
{
float speed = playerController.runSpeed;
direction = playerController.getDirection ();
if(newCameraHeight < cameraHeight)
{
cameraHeight -= 0.1f;
}
else if(newCameraHeight > cameraHeight)
{
cameraHeight += 0.1f;
}
Vector3 MoveTo = new Vector3 (player.transform.position.x, cameraHeight, player.transform.position.z);
switch(direction){
case 0:
MoveTo += new Vector3(0, 0, -cameraDistance);
break;
case 1:
MoveTo += new Vector3(-cameraDistance , 0, 0);
break;
case 2:
MoveTo += new Vector3(0, 0, cameraDistance);
break;
case 3:
MoveTo += new Vector3(cameraDistance , 0, 0);
break;
}
transform.position = Vector3.MoveTowards (transform.position, MoveTo, speed);
Vector3 LookAtVector = new Vector3 (player.transform.position.x, player.transform.position.y + 2, player.transform.position.z);
// Create a rotation based on the relative position of the player being the forward vector.
Quaternion neededRotation = Quaternion.LookRotation(LookAtVector - transform.position);
// Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
transform.rotation = Quaternion.Lerp(transform.rotation, neededRotation, smooth * Time.deltaTime);
}
public void changeHeight(float height)
{
newCameraHeight = height;
}
}
The only insight I can provide is that it may be due to changing the position AND the rotation in the same frame. When you change the position of something that is rendered by a perspective camera, that object's rotation is changed to suit the perspective. Then in the same frame you try to change the rotation again and something inside unity goes kaput for a sec.
nothing pretty... I wonder if using a coroutine would help?
Hey I just fixed $$anonymous$$e by moving some scaling code from Update() to LateUpdate(). Is there anything scaling your transform while you move and rotate it?
Answer by Bunny83 · Nov 05, 2014 at 03:11 PM
Personally i never had an issue like that. The only thing i can think of is that LookRotation has a problem creating a valid rotation since it looks like you're camera is facing upside down and you don't pass an up-vector to LookRotation. That means it uses "world up" (aka Vector3.up) as up vector. If you look straight down it can't determine the rotation around your look direction since the up vector is parallel to your look vector. You might want to calculate a valid up vector for your LookRotation call. The up vector defines how the quaterniion is rotated around the look axis. It doesn't have to be exact 90° to the look axis but it need to be different from your Look axis.