How to turn camera 90 degrees smoothly?

Hello! So I’m super new to unity. I’m using some code I found in a tutorial to make the camera rotate with swipe gestures via a Leap Motion. However, the code makes the camera rotate continuously without stopping. How would I get it to rotate smoothly 90 degrees per swipe gesture? (Note: the camera is being assigned to the name blockOne. It was just a placeholder from the tutorial)

using UnityEngine;
using System.Collections;
using Leap;

public class GestureHands : MonoBehaviour
{
    Controller controller;

    public GameObject blockOne;

    public bool spinLeft;
    public bool spinRight;


    void Start()
    {
        controller = new Controller();
        controller.EnableGesture(Gesture.GestureType.TYPESWIPE);
        controller.Config.SetFloat("Gesture.Swipe.MinLength", 100.0f);
        controller.Config.SetFloat("Gesture.Swipe.MinVelocity", 150f);
        controller.Config.Save();



    }


    void Update()
    {

        Frame frame = controller.Frame();
        GestureList gestures = frame.Gestures();
        Hand hand = frame.Hands.Frontmost;
        if (hand.IsRight)
        {
            for (int i = 0; i < gestures.Count; i++)
            {
                Gesture gesture = gestures*;*

if (gesture.Type == Gesture.GestureType.TYPESWIPE)
{
SwipeGesture Swipe = new SwipeGesture(gesture);
Vector swipeDirection = Swipe.Direction;
if (swipeDirection.x < 0)
{
Debug.Log(“Left”);
spinLeft = true;
spinRight = false;

}
else if (swipeDirection.x > 0)
{
Debug.Log(“Right”);
spinRight = true;
spinLeft = false;
}

}
}

if (spinLeft == true)
{
blockOne.transform.Rotate(Vector3.down, 45 * Time.deltaTime);
}

if (spinRight == true)
{
blockOne.transform.Rotate(Vector3.up, 45 * Time.deltaTime);
}

}

}
}

Hi @IxiArcana - Doing a similar thing to you. Originally i used an IEnumerator to create a time delay after which id falsify the bool that makes the camera rotate:

However this led to a major bug with the way the swipe gesture works so id recomend an alternative approach:

-Set the camera to ‘LookAt’ a target gameobject(make it empty/invisible) that moves to the new position when you perform a swipe. Im using a ‘smoothLookAt’ pre-built script for a smooth rotation effect.

  • When you swipe, call the command to re-position the target gameobject so the camera will rotate to look in the desired direction.

  • (apologies for messy script)

    using UnityEngine;
    using System.Collections;
    using Leap;

    public class GestureSwipe : MonoBehaviour {

     Controller controller;
     public GameObject User;
     public Transform CenterTransform;
     public GameObject CenterObject;
     public Transform Spring;
     public Transform Summer;
     public Transform Autumn;
     public Transform Winter;
     public bool spinLeft;
     public bool spinRight;
     public bool cameraMoveSp2Su;
     public bool cameraMoveSp2Wi;
     public bool cameraMoveSu2Au;
     public bool cameraMoveSu2Sp;
     public bool cameraMoveAu2Su;
     public bool cameraMoveAu2Wi;
     public bool cameraMoveWi2Sp;
     public bool cameraMoveWi2Au;
    
    
     void Start () {
    
         
         controller = new Controller();
         controller.EnableGesture(Gesture.GestureType.TYPESWIPE);
         controller.Config.SetFloat("Gesture.Swipe.MinLength", 200.0f);
         controller.Config.SetFloat("Gesture.Swipe.MinVelocity",2000.0f);
         controller.Config.Save();
         
     }
    
    
     void Update()
     {
    
         if( cameraMoveSp2Su==true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Summer.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
        else if (cameraMoveSp2Wi == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Winter.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveSu2Sp == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Spring.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveSu2Au == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Autumn.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveAu2Wi == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Winter.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveAu2Su == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Summer.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveWi2Sp == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Spring.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         else if (cameraMoveWi2Au == true)
         {
    
             CenterTransform.position = Vector3.MoveTowards(CenterTransform.position, Autumn.position, 10.0f * Time.deltaTime);
    
    
    
         }
    
         Frame frame = controller.Frame();
             GestureList gestures = frame.Gestures();
    
             for (int i = 0; i < gestures.Count; i++)
             {
                 Gesture gesture = gestures*;*
    

if (gesture.Type == Gesture.GestureType.TYPESWIPE)
{
SwipeGesture Swipe = new SwipeGesture(gesture);
Vector swipeDirection = Swipe.Direction;
float swipeSpeed = Swipe.Speed;
Vector swipeStart = Swipe.StartPosition;
if (CenterTransform.position == Spring.position)
{
if (swipeDirection.x < 0)
{

Debug.Log(“Left”);
cameraMoveSp2Su = true;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;

}

else if (swipeDirection.x > 0)
{

Debug.Log(“Right”);

cameraMoveSp2Su = false;
cameraMoveSp2Wi = true;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;

}

}

else if (CenterTransform.position == Summer.position)
{
if (swipeDirection.x < 0)
{

Debug.Log(“Left”);
cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = true;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;

}

else if (swipeDirection.x > 0)
{

cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = true;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;

Debug.Log(“Right”);

}

}

else if (CenterTransform.position == Autumn.position)
{
if (swipeDirection.x < 0)
{

Debug.Log(“Left”);

cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = true;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;
}

else if (swipeDirection.x > 0)
{

cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = true;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = false;
Debug.Log(“Right”);

}

}

else if (CenterTransform.position == Winter.position)
{
if (swipeDirection.x < 0)
{

Debug.Log(“Left”);

cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = true;
cameraMoveWi2Au = false;
}

else if (swipeDirection.x > 0)
{

cameraMoveSp2Su = false;
cameraMoveSp2Wi = false;
cameraMoveSu2Au = false;
cameraMoveSu2Sp = false;
cameraMoveAu2Su = false;
cameraMoveAu2Wi = false;
cameraMoveWi2Sp = false;
cameraMoveWi2Au = true;
Debug.Log(“Right”);

}

}

}

}
}

}

Hope this helps!