- Home /
How can I make it so cameraCenter Roatates 90 degrees smoohly after swipe!
Hello, I have been trying to make it so after I swipe it rotates the cameraCenter Transform 90 degrees, I got the swipe working but I could not manage to make or find a script that rotates it 90 degrees smoothly, so it takes 0.3 seconds till it does the 90 degrees turn. And also that it can turn -90 when I swipe right and +90 when I swipe left. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Swipe : MonoBehaviour
{
private Vector2 fingerDown;
private Vector2 fingerUp;
public bool detectSwipeOnlyAfterRelease = false;
public Transform CenterCamera;
public float SWIPE_THRESHOLD = 15f;
// Update is called once per frame
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerUp = touch.position;
fingerDown = touch.position;
}
//Detects Swipe while finger is still moving
if (touch.phase == TouchPhase.Moved)
{
if (!detectSwipeOnlyAfterRelease)
{
fingerDown = touch.position;
checkSwipe();
}
}
//Detects swipe after finger is released
if (touch.phase == TouchPhase.Ended)
{
fingerDown = touch.position;
checkSwipe();
}
}
}
void checkSwipe()
{
//Check if Horizontal swipe
if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
{
//Debug.Log("Horizontal");
if (fingerDown.x - fingerUp.x > 0)//Right swipe
{
OnSwipeRight();
}
else if (fingerDown.x - fingerUp.x < 0)//Left swipe
{
OnSwipeLeft();
}
fingerUp = fingerDown;
}
//No Movement at-all
else
{
//Debug.Log("No Swipe!");
}
}
float verticalMove()
{
return Mathf.Abs(fingerDown.y - fingerUp.y);
}
float horizontalValMove()
{
return Mathf.Abs(fingerDown.x - fingerUp.x);
}
//////////////////////////////////CALLBACK FUNCTIONS/////////////////////////////
void OnSwipeLeft()
{
Debug.Log("Swipe Left");
//here
}
void OnSwipeRight()
{
Debug.Log("Swipe Right");
//here
}
}
Hope you all can help, spent 5-6 hours trying to make/find a smooth spinning script.
Answer by ray2yar · Dec 27, 2018 at 12:20 AM
I would use a Vector3 to store the current rotation and a Vector3 to store the desired rotation and then lerp between them. You can replace my "DoRotation" with a coroutine instead. Hope this gets you started.
Vector3 currentRotation;
Vector3 desiredRotation;
float amount;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
amount = 0;
desiredRotation = currentRotation + new Vector3(0, 90, 0);
DoRotation();
}
if (Input.GetButtonDown("Fire2"))
{
amount = 0;
desiredRotation = currentRotation + new Vector3(0, -90, 0);
DoRotation();
}
}
void DoRotation()
{
amount += 0.01f;
if(amount > 1)
{
currentRotation = desiredRotation;
} else
{
Invoke("DoRotation", 0.01f);
}
Camera.main.transform.eulerAngles = Vector3.Lerp(currentRotation, desiredRotation, amount);
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Why the player is not rotating and moving at the same time ? 0 Answers
problem after rotate an object 0 Answers
Objects not rotating right half the time based on another object 0 Answers
Smooth Rotation 2 Answers