- Home /
Question by
Cyberbooster · Feb 01, 2012 at 08:39 PM ·
c#camerarotation
Camera Script - RtS style, rotation issues
Here is my script:
using UnityEngine;
using System.Collections;
public class CameraRTS : MonoBehaviour
{
public int ScrollSpeed = 100;
public int ZoomSpeed = 25;
public int ZoomMin = 5;
public int ZoomMax = 100;
public int PanSpeed = 50;
public int PanAngleMin = 25;
public int PanAngleMax = 80;
public string forward = "z";
public string backward = "s";
public string left = "a";
public string right = "d";
//variabiles for rotating the camera
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 250;
public float sensitivityY = 250;
public float minimumX = -360;
public float maximumX = 360;
public float minimumY = -270;
public float maximumY = 90;
// Update is called once per frame
void LateUpdate()
{
// Init camera translation for this frame.
var translation = Time.deltaTime*ScrollSpeed;
// Zoom in or out
var zoomDelta = Input.GetAxis("Mouse ScrollWheel")*ZoomSpeed*Time.deltaTime;
if (zoomDelta!=0)
{
//In order to Zoom, there is some limits
if(camera.fieldOfView - ZoomSpeed * zoomDelta > ZoomMin && camera.fieldOfView - ZoomSpeed * zoomDelta < ZoomMax ){
camera.fieldOfView -= ZoomSpeed * zoomDelta;
}
}
// Move camera with arrow keys
if (Input.GetKey(left)){
var movement = new Vector3 (-translation, 0, 0);
transform.Translate(movement, Space.World);
}
if (Input.GetKey(backward)){
var movement = new Vector3 (0, 0, -translation);
transform.Translate(movement, Space.World);
}
if (Input.GetKey(right)){
var movement = new Vector3 (translation, 0, 0);
transform.Translate(movement, Space.World);
}
if (Input.GetKey(forward)){
var movement = new Vector3 (0, 0, translation);
transform.Translate(movement, Space.World);
}
//Rotate the camera while pressing "Backspace" key MUST BE FIXED
if(Input.GetKey(KeyCode.Backspace)){
float rotationY = 0F;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}//end "Backspace" section
}//LateUpdate
}//Class
The camera moves correctly, zoom ok. But when I have to rotate it (while pressing Backspace), it is impossible to view anything: looks like an earthquake is happening!
Some suggestions?
Comment