Quaternion Lerp Camera (Very beginner request)

Hi !

I’m trying to make my camera rotate when you press a button (end of turn button).
I tryed to use Quaternion.Lerp. It works but only once ! Can someone explain me why ?

I mean, the rotation work the second time. But it directly go from 0 to 180 in 1 frame…

Thanks a lot !

using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour {

    public GameController gc;

    Quaternion from;
    Quaternion to;

    void Start () {
      
        from = Quaternion.Euler(0,0,0);
        to = Quaternion.Euler(0,0,180);

    }

    void Update () {
        if (gc.player_turn == 1) {
            transform.rotation = Quaternion.Lerp (from, to, Time.time * 0.4f);
        } else if (gc.player_turn == 2) {
            transform.rotation = Quaternion.Lerp (to, from, Time.time * 0.4f);
        }
    }
}

Time.time is the time since the start of the game (i.e. time since you pressed the play button at the top of the editor).

Lerp is just a math function to work out a mid point between two values, if the third parameter is greater than 1 it’ll just give you the final position.

Put the two together, and if you “push the button” after more than 2.5seconds of runtime you’ll just zip to the final value in one frame.

Try including some way of knowing when the button was pressed so you can work from that instead of the beginning of the game.

1 Like

Thanks a lot it’s fixed !

I’ve stock Time.time in a var “x” when button pressed in my GameController gc.
Then just had to change "Time.time * 0.4f" to “(Time.time-gc.x)*0.4f”.

:smile:

See you !

(and sorry for bad english)