- Home /
How Can I make the camera stop reorienting itself?
I am currently making a game in which the player doesn't need down to be down,and up to be up. Every time I look too far down (As if to be upside down now) it reorients itself so up is up. Same with looking too far up. As far as i can tell, i have no code interfering with its orientation. I'm wondering if it is something that the camera does on its own, and if i can override it somehow.
*edit i got this straight from the widget model game w/ minor changes. ie it doesnt need height becuase the camera can rotate with the player
var target : Transform;
var distance = 10.0;
//var heightDamping = 2.0;
var rotationDamping = 3.0;
var distanceDampingX = 0.5;
var distanceDampingZ = 0.2;
var camSpeed = 2.0;
var smoothed = true;
function LateUpdate () {
// Check to make sure a target has been assigned in Inspector
if (!target)
return;
// Calculate the current rotation angles, positions, and where we want the camera to end up
wantedRotationAngle = target.eulerAngles.y;
//wantedHeight = target.position.y + height;
wantedDistanceZ = target.position.z - distance;
wantedDistanceX = target.position.x - distance;
currentRotationAngle = transform.eulerAngles.y;
//currentHeight = transform.position.y;
currentDistanceZ = transform.position.z;
currentDistanceX = transform.position.x;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the distance
//currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * //Time.deltaTime);
currentDistanceZ = Mathf.Lerp(currentDistanceZ, wantedDistanceZ, distanceDampingZ * Time.deltaTime);
currentDistanceX = Mathf.Lerp(currentDistanceX, wantedDistanceX, distanceDampingX * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the new position of the camera
transform.position -= currentRotation * Vector3.forward * distance ;
transform.position.x = currentDistanceX;
transform.position.z = currentDistanceZ;
//transform.position.y = currentHeight;
// Make sure the camera is always looking at the target
LookAtMe();
}
function LookAtMe(){
//check whether we want the camera to be smoothed or not - can be changed in the Inspector
if(smoothed)
{
//Find the new rotation value based upon the target and camera's current position. Then interpolate
//smoothly between the two using the specified speed setting
var camRotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, camRotation, Time.deltaTime * camSpeed);
}
//This default will flatly move with the targeted object
else{
transform.LookAt(target);
}
}
@script AddComponentMenu("Player/Smooth Follow Camera")
*edit
and yes it is a child of the an object becuase i wanted it to rotate with the player while it is looking at it
The camera does not reorient itself, so it is something being done by code somewhere. If you post your code, someone on this list might be able to help. Note if you are believing the camera is being reoriented based on angle changes in the Inspector, then the issue is likely the a change in Euler angle representation, not any real rotation.
Is your camera a child of something? I did that myself to experiment and it made it so that any changes to the adult screws around with the camera big time, because the camera's position and rotation become relative to the thing it is the child of, ins$$anonymous$$d of the Scene.
I'm not sure under what situation you are having trouble, but I strongly suspect this is a eulerAngles issue. eulerAngles are not stored by Unity. When you set eulerAngles, they are converted to a Queaternion. When you read back the value, it is converted from the Queaternion back to an Euler angle representation. And that representation is not necessarily the same as the one set. For example execute this code:
transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
The result will be (0,180,180). So for example if you are allowing the object this script is attached to rotate to 180 on the 'x' axis, you can see how reading eulerAngles.y on line 23 will not produce the value the code above is expecting.