- Home /
I am getting a problem with moving and rotating a camera!
Basically when I try to rotate the camera to -90 in the Y axis the camera moves to where I tell it but won't rotate properly. Any Ideas? My Code in c#:
// Update is called once per frame
void Update ()
{
if(EscClicked == false)
{
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Position = "Second";
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
Position = "Third";
}
if(Input.GetKeyDown(KeyCode.Alpha4))
{
Position = "Fourth";
}
if(Input.GetKeyDown(KeyCode.Alpha1 ))
{
Position = "First";
}
if(Position == "First")
{
DistanceX = Player.transform.position.x;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z - 10;
RotationX = 0;
RotationY = 0;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = new Quaternion(RotationX, RotationY, RotationZ, 0);
Side1.GetComponent<MeshRenderer>().enabled = false;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = true;
}
if(Position == "Second")
{
DistanceX = Player.transform.position.x;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z + 10;
RotationX = 0;
RotationY = 180;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = new Quaternion(RotationX, RotationY, RotationZ, 0);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = false;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = true;
}
if(Position == "Third")
{
DistanceX = Player.transform.position.x + 10;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z;
RotationX = 0;
RotationY = -90;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = new Quaternion(RotationX, RotationY, RotationZ, 0);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = false;
Side4.GetComponent<MeshRenderer>().enabled = true;
Debug.Log(Camera1.transform.rotation);
}
if(Position == "Fourth")
{
DistanceX = Player.transform.position.x;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z;
RotationX = 0;
RotationY = 0;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = new Quaternion(RotationX, RotationY, RotationZ, 0);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = false;
}
if(Input.GetKeyDown(KeyCode.Escape))
{
EscClicked = true;
Player.GetComponent<CharacterController>().enabled = false;
Player.GetComponent<Rigidbody>().isKinematic = true;
}
}
Comment
Best Answer
Answer by GerryM · Dec 01, 2012 at 09:47 PM
You are trying to use Quaternions as an Euler rotation.
Unity does store rotations (internally) in quaternions, but what you see in the inspector (and what is more intuitive for most people) are euler rotations. Naming the first 3 dimensions of a quaternion x, y, and z does add to the confusion.
So, just change:
Camera1.transform.rotation = new Quaternion(RotationX, RotationY, RotationZ, 0);
to something like:
Camera1.transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);