• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by IxiArcana · Nov 11, 2015 at 01:24 PM · camerarotationcamera-movementgestures

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[i];
                 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);
             }
 
         }
 
 
     }
 }
 
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kempipoo · May 17, 2016 at 05:18 PM

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: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

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[i];
                 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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

MouseOrbitImproved slanted orbit 0 Answers

Clamping when Zoom changes? 0 Answers

Switching Cameras, Movement not working. 1 Answer

Camera rotate X degree based on player rotate 0 Answers

Camera Rotation Switching Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges