Hey,
I’ve set up some basic script to rotate my camera via ‘WASD’ keys rather than by mouse. They alter the euler.Rotation
values when pressed. I also added script that sets the ‘z’ value of the rotation to ‘0’ after every update, to cancel out any unwanted rolling. This works fine, except when the camera faces directly up or down it gets stuck there.
I’m trying to work out why this could be happening. It seems pretty clear that something goes wrong with the Z rotation when the camera is facing either up or down, but I have no idea what exactly is going wrong. I guess a quick fix would be to limit the rotation to just below 90 and -90 degrees, but I’d just like to know if there’s a cleaner solution.
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour
{
void Start ()
{
}
void Update ()
{
rotationX = transform.eulerAngles.x;
rotationY = transform.eulerAngles.y;
rotationZ = transform.eulerAngles.z;
// Rotates for WASD input, multiplied by deltaTime for consistency.
if(Input.GetKey("w") == true)
{
transform.Rotate (Vector3.left * (Time.deltaTime * 50));
}
if(Input.GetKey("s") == true)
{
transform.Rotate (Vector3.right * (Time.deltaTime * 50));
}
if(Input.GetKey("a") == true)
{
transform.Rotate (Vector3.down * (Time.deltaTime * 50));
}
if(Input.GetKey("d") == true)
{
transform.Rotate (Vector3.up * (Time.deltaTime * 50));
}
Vector3 zRotationStatic = transform.eulerAngles; // Sets the z value of eulerAngles to 0.
zRotationStatic.z = 0;
transform.eulerAngles = zRotationStatic;
}
}
UPDATE: In case anyone out there winds up wanting to achieve the same thing I am, turns out, it sort of fixed itself! I implemented a new way of working the camera, involving a ‘Camera Focus’ object, which you position wherever you want camera to orbit around, and which is also the parent of the camera itself.
My new code does a few things. To rotate sideways with ‘A’ and ‘D’, the Camera Focus is rotated around the ‘z’ axis, and the camera is rotated with it. To rotate up and down with ‘W’ and ‘S’, the Camera is rotated around the Camera Focus, using transform.RotateAround([POSITION ROTATED AROUND], [AXIS ROTATED ON], [DEGREES])
. Note that the camera rotates up and down, but not the camera focus. It’s sort of like a gyroscope.
public class CameraMovement : MonoBehaviour
{
public int cameraMoveSpeed;
GameObject cameraFocus; // For referencing the point around which the camera rotates.
Transform cameraFocusTransform; // For referencing the Camera Focus' transform.
void Start ()
{
cameraFocus = GameObject.FindWithTag("CameraFocus");
cameraFocusTransform = cameraFocus.transform;
}
void Update ()
{
// Responds to keystrokes with camera rotation.
/* Left and right rotations. These rotations are applied to the Camera Focus. This way, the Camera Focus'
* lateral rotation axis rotates with it, and therefore with the camera. This way, the up and down rotations
* always work. */
if(Input.GetKey("a") == true)
{
cameraFocusTransform.Rotate(cameraFocusTransform.up, cameraMoveSpeed * Time.deltaTime);
}
if(Input.GetKey("d") == true)
{
cameraFocusTransform.Rotate(cameraFocusTransform.up, -cameraMoveSpeed * Time.deltaTime);
}
/* Up and down rotations. These rotations are performed with the RotateAround function, so that the camera rotates,
* rather than the Camera Focus.
* NOTE: cameraFocusTransform.[axis] may need to be changed depending on the orientation that the camera starts in
* relative to the Camera Focus. */
if(Input.GetKey("w") == true)
{
transform.RotateAround(cameraFocusTransform.position, cameraFocusTransform.right, cameraMoveSpeed * Time.deltaTime);
}
if(Input.GetKey("s") == true)
{
transform.RotateAround(cameraFocusTransform.position, cameraFocusTransform.right, -cameraMoveSpeed * Time.deltaTime);
}
}
}