Rotation on key press

I am new to both game development and Unity as I am starting out by building a basic game. The game involved a 3D world with an orthagraphic view to give an illusion of being in 2D. When the gamer press the spacebar, it rotates 90 degrees just like in Fez.

How can I achieve this? I am also not too sure if I should rotate the camera or the objects.

At the moment I am using this code that does not work.

var minAngle = 0.0;
var maxAngle = 90.0;
var rotate = false;

function Update () {
	if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.X)) 
	{
		rotate = true;
	}
	
	if (rotate == true)
	{
		var angle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
		Debug.Log(angle);
		transform.eulerAngles = Vector3(0, angle, 0);
		if (angle >= 90.0)
			rotate = false;
	}
}

Here is a solution that rotates by 90 degrees every key press. Note sure what your intent was…one time rotation, or to continue to rotate. If you want your camera to rotate around your scene, you can put an empty game object at the center of the scene, make the camera a child, and put this script on an empty game object. Or you can explore using Transform.RotateAround() instead of this code:

#pragma strict

var rotAmount = 90.0;
var destEuler = Vector3.zero;
var speed = 5.0;
private var currEuler;

function Start() {
    currEuler = destEuler;
	transform.eulerAngles = destEuler;
}
 
function Update () {
    if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.X))  {
       destEuler.y += rotAmount;
    }
 
   currEuler = Vector3.Lerp(currEuler, destEuler, Time.deltaTime * speed);
   transform.eulerAngles = currEuler;
}

There are multiple Euler angle representations for any given physical rotation. And the rotation (Vector3) you assign to a transform.eulerAngle, is not necessarily the same one you get back if you immediately read the same transform.eulerAngle. So it is best to avoid reading eulerAngles (directly or as a parameter). That is why this code uses (and changes) currEuler rather than directly using transform.eulerAngles.

I know this is an old question, but I just translated the first answer to C# for my own purposes and figured I’d share the code for others who find this question:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    public float rotationAmount;
    public float rotationSpeed;
    public Vector3 destEuler = new Vector3(0,0,0);
    private Vector3 currEuler = new Vector3(0,0,0);

	// Use this for initialization
	void Start () {

        rotationAmount = 90.0f;
        rotationSpeed = 10.0f;
        transform.eulerAngles = destEuler;
	}


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

        if (Input.GetKeyDown(KeyCode.RightArrow)) {
            destEuler.y += rotationAmount;
        }

        currEuler = Vector3.Lerp(currEuler, destEuler, Time.deltaTime * rotationSpeed);
        transform.eulerAngles = currEuler;
        

	}
}

To have the camera like in FEZ, only the scenery would have to rotate

Try to use transform.rotation = Quaternion.Euler(90,0,0);

90 - x, 0 - y , 0 - z.

As Quanturn2 said, you would need to make the camera stay still but all the scenery rotates. First of all you should start with tagging all your scenery as scenery. Then simply do something like this script below does.

	var scenery : GameObject[];
        var minAngle = 0.0;
        var maxAngle = 90.0;
        var rotate = false;

function Start()
{
        scenery = GameObject.FindGameObjectsWithTag("scenery");
        //scenery is now a group of GameObjects that are tagged scenery.
}

 
function Update () {
    if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.X)) 
    {
       rotate = true;
    }
 
    if (rotate == true)
    {
       var angle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
       Debug.Log(angle);
       scenery.transform.eulerAngles = Vector3(0, angle, 0);
       if (angle >= 90.0)
         rotate = false;
    }
}

But don’t use this code yet… for the best results you probably want to make all the scenery one big object tagged scenery rather then plenty small ones. Or you could tag only one of the scenery objects as scenery and parent all the rest to it. The code above has not been tested so i’m sorry if something goes wrong.