Rotating a Camera like this game

So I’ve been playing some old games on Ludum Dare while I’ve been waiting for the 25th and I stumbled upon this game:

TransDimensional Moon Rift by Shigor

http://dl.dropbox.com/u/19916467/TDMR/WebPlayer.html

His game has a very nice Camera rotating mechanic where by pressing either “Q” or “E” makes the camera turn at a 90 degree angle. I have wanted to do the same using the “MouseLook” script supplied by Unity but I have no luck in doing so :confused:

I have downloaded his source code inorder to figure it out but I can’t seem to isolate the code into a script itself without errors :confused:

Anyone know how to re-create it?

A coroutine is something i would do.
keep your start rotation of the Y axis and rotate it to the left \ right until it reaches the start rotation + 90.

something like this:

    private Transform t;

    private void Awake()
    {
        t = transform;
    }

    private IEnumerator Rotate90Deg(bool right, float speed)
    {
        float yAxisRot = t.eulerAngles.y;
        float timeItTakes = Time.time + 90 / speed;        

        if (right)
            do
            {
                t.eulerAngles += Vector3.up * speed * Time.deltaTime;

                yield return 0;
            }
            while (Time.time < timeItTakes);

        else
            do
            {
                t.eulerAngles -= Vector3.up * speed * Time.deltaTime;

                yield return 0;
            }
            while (Time.time < timeItTakes);
    }

Did not check it so not sure it will compile and work but should give you the way of doing it.

^ not like that.

I cant look at the webplayer though, so im possibly going to steer you wrong here… anyway…

Quaternion targetRotation;
float speed = 5;

void Start()
{
  FaceDirection(0);
}

//0-forward, 1-right, 2-back, 3-left
void FaceDirection(int direction)
{
   targetRotation = Quaternion.Euler(0, direction*90.0f, 0);
}

void Update()
{
  transform.RotateTowards(transform.rotation, targetRotation, speed * Time.deltaTime);
}

Any simple ways of doing it (non PHP)? Thank you for your contributions, but I still a newbie to C# and Javascript :stuck_out_tongue:

I dont think it gets much simpler than the code I posted (C#)

why not?

thats not PHP, thats C#.

The code you wrote can be achieved in one line, and looking at your code, im about 99% sure its not going to work anyway…

one line but inefficient.
Even though i never checked it, lemme check if it works…
k its fixed.

Oh, I think Spanda was commenting on how it’s labelled as “PHP Code” rather than “Code.”

lol… so how exactly is one line more inefficient than 20 lines that include using a co-routine?

Its not about the numbers of lines… Its about how many times those lines execute.
In your example you run forever something that should berely run. On my example you run it only when you need to. So tell me how is rotating the camera every frame on your game when youre not really changing its rotation is efficient?
Also the more code you have on the update function, the harder it gets to take care of new code