- Home /
Camera rotation does not work properly
Hi, guys!
I'm trying to rotate the camera and make it move in a certain direction on KeyPressed. The problem is - it is not rotating properly. I'm giving the values of rotation of the camera the way it is shown in the Inspector by the function Transform.Rotate. But it is rotating 180 degrees all the time and only with Left and Right buttons. I think that it just takes a previous position of the camera as the reference instead of establishing a rotation parameters every time. Could you check it out, please, and suggest how I could solve it. Thank you!!
The Code:
using UnityEngine; using System.Collections;
public class Navigation : MonoBehaviour {
bool moveCamUp = false;
bool moveCamRght = false;
bool moveCamDwn = false;
bool moveCamLft = false;
bool waitActive = false;
GenerateRoom updateRoom;
void Start () {
updateRoom = GameObject.Find("MainRoom").GetComponent("GenerateRoom") as GenerateRoom;
Quaternion cameraRotation = Camera.mainCamera.transform.localRotation;
}
void Update () {
//UpArrow
if (Input.GetKeyDown (KeyCode.UpArrow)) {
moveCamUp = true;
Camera.mainCamera.transform.Rotate(new Vector3 (0f, 0f, 0f));
}
if (moveCamUp == true) {
Vector3 currentpos = Camera.mainCamera.transform.localPosition;
if (currentpos.z > 27f) {
Camera.mainCamera.transform.localPosition = new Vector3 (0f, 1.5f, -10f);
updateRoom.DestroyObjects ();
updateRoom.CreateObjects ();
updateRoom.ChangeTexture ();
}
currentpos.z += 0.06f;
Camera.mainCamera.transform.localPosition = currentpos;
if (currentpos.z <= 0.05f && currentpos.z >= -0.05f )
moveCamUp = false;
}
//DownArrow
if (Input.GetKeyDown (KeyCode.DownArrow)) {
moveCamDwn = true;
Camera.mainCamera.transform.Rotate(new Vector3 (0f, 180f, 0f));
}
if (moveCamDwn == true) {
Vector3 currentpos = Camera.mainCamera.transform.localPosition;
if (currentpos.z < -27f) {
Camera.mainCamera.transform.localPosition = new Vector3 (0f, 1.5f, 10f);
updateRoom.DestroyObjects ();
updateRoom.CreateObjects ();
updateRoom.ChangeTexture ();
}
currentpos.z -= 0.06f;
Camera.mainCamera.transform.localPosition = currentpos;
if (currentpos.z <= 0.05f && currentpos.z >= -0.05f )
moveCamDwn = false;
}
//RightArrow
if (Input.GetKeyDown (KeyCode.RightArrow)) {
moveCamRght = true;
Camera.mainCamera.transform.Rotate(new Vector3 (0f, 90f, 0f));
}
if (moveCamRght == true) {
Vector3 currentpos = Camera.mainCamera.transform.localPosition;
if (currentpos.x > 27f) {
Camera.mainCamera.transform.localPosition = new Vector3 (-10f, 1.5f, 0f);
updateRoom.DestroyObjects ();
updateRoom.CreateObjects ();
updateRoom.ChangeTexture ();
}
currentpos.x += 0.06f;
Camera.mainCamera.transform.localPosition = currentpos;
if (currentpos.x <= 0.05f && currentpos.x >= -0.05f )
moveCamRght = false;
}
//LeftArrow
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
moveCamLft = true;
Camera.mainCamera.transform.Rotate(new Vector3 (0f, 270f, 0f));
}
if (moveCamLft == true) {
Vector3 currentpos = Camera.mainCamera.transform.localPosition;
if (currentpos.x < -27f) {
Camera.mainCamera.transform.localPosition = new Vector3 (10f, 1.5f, 0f);
updateRoom.DestroyObjects ();
updateRoom.CreateObjects ();
updateRoom.ChangeTexture ();
}
currentpos.x -= 0.06f;
Camera.mainCamera.transform.localPosition = currentpos;
if (currentpos.x <= 0.05f && currentpos.x >= -0.05f )
moveCamLft = false;
}
}
}
Answer by molokolom · May 28, 2014 at 05:44 PM
I'm not sure that it's exactly what I need, but thank you Andres!!
Answer by Andres-Fernandez · May 28, 2014 at 09:37 AM
With Transform.Rotate you are just always applying the same rotation. You need to apply a rotation over time.
I suggest you have a look at the orbit script and try to get whatever you need from there (you basically need the rotation, you don't need the position and distance stuff). Something like:
public float xSpeed = 240.0f; // Set your speed here
private float x = 0.0f;
public void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
}
public void LateUpdate () {
x -= Input.GetAxis("Horizontal") * xSpeed * Time.deltaTime;
Quaternion rotation = Quaternion.Euler(0.0f, x, 0.0f);
transform.rotation = rotation;
}
Your answer
Follow this Question
Related Questions
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer
An alternative to RotateAround for collisions 0 Answers
Issue with transform rotations and local rotations 1 Answer
Clamping cameras with different rotation 0 Answers
How to limit the angle of a camera with transform.RotateAround? 1 Answer