So I’ve got a camera that pans around when your finger drags across the screen, it’s okay apart from when you let go the camera stops instantly, I’d like the camera to keep moving for a little while afterwards, similar to Google Streetview or those 360 YouTube videos but I’m just not sure the right way of going about this. Any help would be greatly appreciated!
Here’s what I’m working with:
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public bool invertX = false;
public bool invertY = false;
void OnEnable () {
print ("TouchLook On");
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
GetComponent<SmoothResetCam> ().enabled = false;
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
rotationZ = invertX ? rotationZ : rotationZ * -1;
float rotationX = delta.y * sensitivityY * Time.deltaTime;
rotationX = invertY ? rotationX : rotationX * -1;
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
}
}
using UnityEngine;
using System.Collections;
public class Inertia : MonoBehaviour
{
public Vector3 speed;
public Vector3 avgSpeed;
private bool dragging = false;
private Vector3 targetSpeedX;
public float rotationSpeed;
public float lerpSpeed;
Touch theTouch;
public Transform refObject;
void Start()
{
dragging = false;
}
void Update()
{
#region Thisis for mobile and touch screen devices (touch input)
//if (Input.GetMouseButtonDown(0))
//{
// dragging = true;
//}
//if (Input.touchCount == 1 && dragging)
//{
// theTouch = Input.GetTouch(0);
// speed = new Vector3(theTouch.deltaPosition.x, theTouch.deltaPosition.y, 0);
// avgSpeed = Vector3.Lerp(avgSpeed, speed, Time.deltaTime * 5);
//}
//else
//{
// if (Input.touchCount == 0 && dragging)
// {
// speed = avgSpeed;
// dragging = false;
// }
// var i = Time.deltaTime * lerpSpeed;
// speed = Vector3.Lerp(speed, Vector3.zero, i);
//}
//refObject.Rotate(Vector3.up * speed.x * rotationSpeed, Space.World);
#endregion
#region This is for PC (mouse input)
if (Input.GetMouseButtonDown(0))
{
dragging = true;
}
if (Input.GetMouseButton(0) && dragging)
{
speed = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0);
avgSpeed = Vector3.Lerp(avgSpeed, speed, Time.deltaTime * 5);
}
else
{
if (dragging)
{
speed = avgSpeed;
dragging = false;
}
var i = Time.deltaTime * lerpSpeed;
speed = Vector3.Lerp(speed, Vector3.zero, i);
}
refObject.Rotate(Vector3.up * speed.x * rotationSpeed, Space.World);
#endregion
}
}
Then add this MoveCamera script and change ‘refObject’ object from this script. This is example from my project. Note that your Camera must be the child of refObject GameObject
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour
{
Ray ray;
RaycastHit hit;
public Inertia inertia;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0) && b)
{
if (hit.collider.name == "Parking")
{
inertia.refObject = cParkingHelper.transform; Camera.main.transform.SetParent(cParkingHelper.transform);
}
if (hit.collider.tag == "ComplexFootball")
{
inertia.refObject = cFootballHelper.transform; Camera.main.transform.SetParent(cFootballHelper.transform);
}
}
}