I'm programming a camera for an RTS and I'm stuck on tilting the camera up and down.
I want the camera to tilt between 20 and 70 degrees using the Z and X keys. This is my code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
//Variables to alter or track in Unity
public float scrollSpeed;
public float increasedScroll;
public float scrollEdge;
public float panSpeed;
public float increasedPan;
public float rotateSpeed;
public float currentZoom;
public float zoomSpeed;
public float zoomRotation;
public Vector2 zoomRange;
public float tiltAngle;
public Vector2 tiltAngleRange;
//Variables for use in script
private Vector3 initialPosition;
private Quaternion initialRotation;
private Vector3 mouse;
//
void Start()
{
initialPosition = transform.position;
initialRotation = transform.rotation;
}
//Each frame
void Update()
{
//BASIC CAMERA MOVEMENT (panning)
//Check for shift key input and change speed accordingly
shiftSpeed(increasedPan, increasedScroll);
//Pan with middle mouse
mouse = Input.mousePosition;
if (Input.GetKey("mouse 2"))
{
panMove(Vector3.right, mouse.x, Screen.width);
panMove(Vector3.forward, mouse.y, Screen.height);
}
//Pan with WASD keys or edge of screen
else
{
if(Input.GetKey("d") || mouse.x >= Screen.width * (1 - scrollEdge))
{
moveCamera(Vector3.right, scrollSpeed);
}
else if (Input.GetKey("a") || mouse.x <= Screen.width * scrollEdge)
{
moveCamera(Vector3.right, -scrollSpeed);
}
if (Input.GetKey("w") || mouse.y >= Screen.height * (1 - scrollEdge))
{
moveCamera(Vector3.forward, scrollSpeed);
}
else if (Input.GetKey("s") || mouse.y <= Screen.width * scrollEdge)
{
moveCamera(Vector3.forward, -scrollSpeed);
}
}
//Camera zoom with scrl wheel (up and down)
currentZoom -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 1000 * zoomSpeed;
currentZoom = Mathf.Clamp(currentZoom, zoomRange.x, zoomRange.y);
transform.position = new Vector3(transform.position.x, transform.position.y - (transform.position.y - (initialPosition.y + currentZoom)) * 0.1f, transform.position.z);
//Camera tilt with ZX keys
if (Input.GetKey("z"))
{
tiltCamera(tiltAngle);
}
else if (Input.GetKey("x"))
{
tiltCamera(-tiltAngle);
}
}
//Occurs after update
void LateUpdate()
{
//Remove shifted spped
shiftSpeed(-increasedPan, -increasedScroll);
}
void shiftSpeed(float panChange, float scrollChange)
{
if (Input.GetKey(KeyCode.LeftShift))
{
panSpeed += panChange;
scrollSpeed += scrollChange;
}
}
void panMove(Vector3 panDirection, float inputDirection, int screenAxis)
{
moveCamera(panDirection, panSpeed * ((inputDirection - screenAxis * 0.5f) / (screenAxis * 0.5f)));
}
void moveCamera(Vector3 direction, float scrlOrPanSpeed)
{
transform.Translate(direction * Time.deltaTime * scrlOrPanSpeed, Space.World);
}
void tiltCamera(float angle)
{
angle = angle + initialRotation.eulerAngles.x;
Quaternion newRotation = Quaternion.AngleAxis(angle, Vector3.left);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 1f);
}
}
At the moment it just tilts up or down by 1 degree from the starting rotation when I press Z and X.
z
orx
your are passing constant value, may be this is the reason, you can check it out by addingtiltAngle = angle
as very last line oftiltCamera
method \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 14:58tiltAngle
variable value? and why it is in there? \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 15:05