Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate the camera smoothly

Discussion in 'Scripting' started by Simonxzx, Jun 23, 2015.

  1. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi everyone, I'm developing a game in which the camera rotates 90 degrees when the player swfts rightward or to the left. Everything I want is to make this movement in a gradual way, becuase actually when someone makes a swipe the camera rotates instantly. Thank you very much. Below's the script.
    [...]

    Code (CSharp):
    1. if (swipeValue > 0) //right swipe
    2.                             {
    3.                                 this.transform.Rotate(0, 90, 0);
    4.                                 Debug.Log("Right");
    5.                             }
    6.                             else if (swipeValue < 0) //left swipe
    7.                             {
    8.                                 this.transform.Rotate(0, -90, 0);
    9.                                 Debug.Log("Left");
    10.                             }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Use Quaternion.RotateTowards and instead of rotating explicitly, set a target rotation variable.

    Something like:
    Code (csharp):
    1.  
    2. // somewhere at the top of your file with the other variables
    3. protected float targetRotation = 0.0f;
    4.  
    5. // down with your other code in LateUpdate()
    6. if (swipeValue > 0) //right swipe
    7. {
    8.      targetRotation = 90.0f;
    9. }
    10. else if (swipeValue < 0) //left swipe
    11. {
    12.      targetRotation = -90.0f;
    13. }
    14.  
    15. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, targetRotation, 0), 45.0f * Time.deltaTime);
    16.  
     
  3. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Okay @GroZZleR , thank you very much for your reply. But in this way when I swipe for example rightward the camera doesn't rotate to 90°, but only a bit. :confused:
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is the code in an Update or LateUpdate function? Can you post your whole script?
     
  5. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    yeah, that's it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class WalkAndSwipe : MonoBehaviour
    6. {
    7.     public RuntimePlatform UsedPlatform = Application.platform;
    8.     public float minSwipeDistY = 90f;
    9.     public float minSwipeDistX = 90f;
    10.     public float Force = 2000f;
    11.     public float timer = 0.0f;
    12.     private float seconds;
    13.     private Vector2 startPos;
    14.     public GameObject Protagonist;
    15.     public Camera MainCamera;
    16.     public TextMesh TimerText;
    17.     public bool EnableTimer = false;
    18.     public bool Enabled = true;
    19.     public float rotationDegreesPerSecond = 45f;
    20.     public float rotationDegreesAmount = 90f;
    21.     private float totalRotation = 0;
    22.     protected float targetRotation = 0.0f;
    23.  
    24.     void Start()
    25.     {
    26.         Force = 0;
    27.     }
    28.     void Update()
    29.     {
    30.         Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force);
    31.         if (EnableTimer == true)
    32.         {
    33.             timer += Time.deltaTime * 100.00f;
    34.             int sec_int = int.Parse(timer.ToString("f0"));
    35.             seconds = sec_int / 100.00f;
    36.             TimerText.text = seconds.ToString("f") + " seconds";
    37.         }
    38.         if (Enabled)
    39.         {
    40.             if (Input.touchCount > 0)
    41.             {
    42.                 Touch touch = Input.touches[0];
    43.                 switch (touch.phase)
    44.                 {
    45.                     case TouchPhase.Began:
    46.                         startPos = touch.position;
    47.                         Debug.Log("Began");
    48.                         Force = 0.0f;
    49.                         Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force);
    50.                         break;
    51.  
    52.                     case TouchPhase.Ended:
    53.  
    54.                         float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
    55.  
    56.                         if (swipeDistVertical > minSwipeDistY)
    57.                         {
    58.                             float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
    59.  
    60.                             if (swipeValue > 0) //up swipe
    61.                             {
    62.                                 Force = 2000f;
    63.                                 Debug.Log("Up");
    64.                             }
    65.                             else if (swipeValue < 0) //down swipe
    66.                             {
    67.                                 Protagonist.transform.Rotate(0, 180, 0);
    68.                                 Force = 2000f;
    69.                                 Debug.Log("Down");
    70.                             }
    71.                         }
    72.  
    73.                         break;
    74.                 }
    75.             }
    76.         }
    77.     }
    78.     void LateUpdate()
    79.     {
    80.         if (Input.touchCount > 0)
    81.         {
    82.             Touch touch = Input.touches[0];
    83.             float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
    84.  
    85.             if (swipeDistHorizontal > minSwipeDistX)
    86.             {
    87.                 float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
    88.  
    89.                 if (swipeValue > 0) //right swipe
    90.                 {
    91.                     targetRotation = 90.0f;
    92.                 }
    93.                 else if (swipeValue < 0) //left swipe
    94.                 {
    95.                     targetRotation = -90.0f;
    96.                 }
    97.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, targetRotation, 0), 45f * Time.deltaTime);
    98.             }
    99.         }
    100.     }
    101. }
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Move
    Code (csharp):
    1. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, targetRotation, 0), 45f * Time.deltaTime);
    to the very bottom of LateUpdate(), outside all the if statements, otherwise it'll only rotate when you have active touches.
     
  7. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    The only problem is that in this way I can only rotate between -90 and 90 degrees, and when I'm for example at -90 degrees rotation, if I swipe left nothing happens; the same thing happens when I'm at 90 degrees. Moreover, the rotation doesn't stop at "0" degrees rotation, but by 90 degrees it directly goes to -90 and from -90 it goes to 90. :(
     
  8. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So you want it to be able to spin 360 degrees? In that case, instead of setting it to -90 and 90, just subtract and add 90 instead.

    Code (csharp):
    1.  
    2.   if (swipeValue > 0) //right swipe
    3.   {
    4.        targetRotation += 90.0f;
    5.   }
    6.   else if (swipeValue < 0) //left swipe
    7.   {
    8.        targetRotation -= 90.0f;
    9.   }
    10.  
     
  9. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Yeah, thank you, I solved this prolem after I wrote it here ahah. But I have another problem now : when I swipe, the system doesn't reckognize only one swipe, but the more I keep my finger on the screen, the more swipes are counted; so, one effective swipe is counted by the system like 5,6,7 or 8 and so on till I don't release my finger from the screen and so the degrees added are not only 90, but 90 * the swipes it counts.
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Not sure how to help with that one unfortunately, I've never used the touch input part of Unity. Hopefully someone comes along that can help you.
     
  11. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Okay, anyway, thank you very much for your tips @GroZZleR !