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.
[…]
if (swipeValue > 0) //right swipe
{
this.transform.Rotate(0, 90, 0);
Debug.Log("Right");
}
else if (swipeValue < 0) //left swipe
{
this.transform.Rotate(0, -90, 0);
Debug.Log("Left");
}
Use Quaternion.RotateTowards and instead of rotating explicitly, set a target rotation variable.
Something like:
// somewhere at the top of your file with the other variables
protected float targetRotation = 0.0f;
// down with your other code in LateUpdate()
if (swipeValue > 0) //right swipe
{
targetRotation = 90.0f;
}
else if (swipeValue < 0) //left swipe
{
targetRotation = -90.0f;
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, targetRotation, 0), 45.0f * Time.deltaTime);
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. 
Is the code in an Update or LateUpdate function? Can you post your whole script?
yeah, that’s it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WalkAndSwipe : MonoBehaviour
{
public RuntimePlatform UsedPlatform = Application.platform;
public float minSwipeDistY = 90f;
public float minSwipeDistX = 90f;
public float Force = 2000f;
public float timer = 0.0f;
private float seconds;
private Vector2 startPos;
public GameObject Protagonist;
public Camera MainCamera;
public TextMesh TimerText;
public bool EnableTimer = false;
public bool Enabled = true;
public float rotationDegreesPerSecond = 45f;
public float rotationDegreesAmount = 90f;
private float totalRotation = 0;
protected float targetRotation = 0.0f;
void Start()
{
Force = 0;
}
void Update()
{
Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force);
if (EnableTimer == true)
{
timer += Time.deltaTime * 100.00f;
int sec_int = int.Parse(timer.ToString("f0"));
seconds = sec_int / 100.00f;
TimerText.text = seconds.ToString("f") + " seconds";
}
if (Enabled)
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
Debug.Log("Began");
Force = 0.0f;
Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force);
break;
case TouchPhase.Ended:
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValue > 0) //up swipe
{
Force = 2000f;
Debug.Log("Up");
}
else if (swipeValue < 0) //down swipe
{
Protagonist.transform.Rotate(0, 180, 0);
Force = 2000f;
Debug.Log("Down");
}
}
break;
}
}
}
}
void LateUpdate()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0) //right swipe
{
targetRotation = 90.0f;
}
else if (swipeValue < 0) //left swipe
{
targetRotation = -90.0f;
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, targetRotation, 0), 45f * Time.deltaTime);
}
}
}
}
Move 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.
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. 
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.
if (swipeValue > 0) //right swipe
{
targetRotation += 90.0f;
}
else if (swipeValue < 0) //left swipe
{
targetRotation -= 90.0f;
}
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.
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.
Okay, anyway, thank you very much for your tips @GroZZleR !