- Home /
90 degree rotation script, cant find or make one that works for my needs.
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 Smehi · Dec 26, 2018 at 09:12 PM
If I understand correctly you want to turn the camera's rotation to be some vector in some amount of time. I've done some other things that happen in x amount of time and I've used Coroutines for this.
public IEnumerator RotateCamera(Vector3 cameraRotation, Vector3 desiredCameraRotation, float time)
{
float t = 0;
while (t < 1)
{
t += Time.deltaTime / time;
Camera.main.transform.rotation = Quaternion.Euler(Vector3.Lerp(cameraRotation,
desiredCameraRotation,
t));
yield return null;
}
}
Then you could call it in OnSwipeLeft()
or OnSwipeRight()
like this:
void OnSwipeRight()
{
Debug.Log("Swipe Right");
// If there is already a rotate coroutine in progress we want that one to stop and start a new one.
if (currentRotateCoroutine != null)
{
StopCoroutine(currentRotateCoroutine);
}
currentRotateCoroutine = RotateCamera(Camera.main.transform.rotation, desiredRotation, rotateTime);
StartCoroutine(currentRotateCoroutine);
}
Sorry, I don't really recall the math behind the time thing so I can't really explain it, but it should work. Hope this helps, good luck!