1
\$\begingroup\$

I want to make a retro-styled game with a modern look to it, and to do so I wish to make a quake-styled camera tilt. By that I mean when moving sideways the FPS Camera tilts a little bit, and the returns to default when letting go of the key. I have little to no scripting knowledge.

https://www.youtube.com/watch?v=scSea_v7JHA

Notice how in the video when moving to the side the camera tilts a little-bit.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Attach the following script to your Camera. You can set the variables from your editor. I would advice you to get into scripting if you want things like this to happen.

EDIT: This code rotates the camera on its Z axis once per frame, when during this frame it detects a KeyDown event. When detecting the KeyUp event it rotates it back to its original Z value, nullifying the tilt.

using UnityEngine;
using System.Collections;

public class CamTilt : MonoBehaviour {

    //Editor variables, you can customize these
    public float _tiltAmount = 5;
    public KeyCode _leftBtn = KeyCode.A; //A is default
    public KeyCode _rightBtn = KeyCode.D; //D is default

    // Update is called once per frame
    void Update () {

         // If _leftBtn key is hit, rotate Z axis of camera by _tiltAmount
        if (Input.GetKeyDown (_leftBtn)) {
            this.transform.Rotate (0, 0, _tiltAmount);
        } else if (Input.GetKeyUp (_leftBtn)) {
            this.transform.Rotate (0, 0, -_tiltAmount);
        }

        // Same as above, but inverted values
        if (Input.GetKeyDown (_rightBtn)) {
            this.transform.Rotate (0, 0, -_tiltAmount);
        } else if (Input.GetKeyUp (_rightBtn)) {
            this.transform.Rotate (0, 0, _tiltAmount);
        }
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Instead of just dumping a piece of code, could you also explain more about what the code does and why it works? That way more people could benefit from the answers as a learning material. \$\endgroup\$
    – user35344
    Commented Sep 19, 2016 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .