3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I don't know if your code is working correctly or not but I can notice one thing that every time on pressing z or x your are passing constant value, may be this is the reason, you can check it out by adding tiltAngle = angle as very last line of tiltCamera method \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 14:58
  • \$\begingroup\$ Yeah, you are right, I am aware that that bit is wrong, I just don't know how to fix it :( I know how to do a rotation, and how to control a smooth rotation using input. I just cannot figure out a way to clamp the rotation between 20 and 70 degrees. \$\endgroup\$ – Patrick Thorpe Jan 27 '16 at 15:00
  • \$\begingroup\$ I did mention that, by adding a line of code \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 15:02
  • \$\begingroup\$ I added the code line you mentioned and now the camera just flips around erratically when i press z or x \$\endgroup\$ – Patrick Thorpe Jan 27 '16 at 15:05
  • \$\begingroup\$ btw what is your tiltAngle variable value? and why it is in there? \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 15:05
2
\$\begingroup\$

Well, to achieve your requirement, I have another work around, place these lines of code in very same script.

...

public float _cameraRotationSpeed = 0.1f;

...
void Update ()
    {
        ...
        ...

        if (Input.GetKey ("z")) {
            tiltCamera (true); // Replace parameter with bool
        } else if (Input.GetKey ("x")) {
            tiltCamera (false); // Replace parameter with bool
        }

        ...
        ...
    }

// Replace your tilt camera method by

void tiltCamera (bool rotateRight)
    {
        // Y represents rotation around Y-axis and will rotate in x-z axes. You can modify it.

        float angle = transform.eulerAngles.y;
        if (rotateRight)
            angle += _cameraRotationSpeed;
        else
            angle -= _cameraRotationSpeed;
        angle = Mathf.Clamp (angle, 20, 70);

        // Whatever reference you got in angle change exactly that same axis. For now Y rotation is being changed.
        transform.eulerAngles = new Vector3 (transform.eulerAngles.x, angle, transform.eulerAngles.z);
    }
...
\$\endgroup\$
  • \$\begingroup\$ This works great :) I want the camera to move up and down not left and right but I can fix that just fine. Thanks for the help. \$\endgroup\$ – Patrick Thorpe Jan 27 '16 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.