Hi
I found this script online for an orbiting camera using touch controls and it works perfectly fine. But I would like the movement to be a little bit smoother and not stop instantly when lifting the finger.
Anyone got an idea how to do that?
{
private float x;
private float y;
public float xspeed;
public float yspeed;
private Touch touch;
public float distance = 10;
public Transform target;
Rect margin;
public float perspectiveZoomSpeed = 0.1f;
public float orthoZoomSpeed = 0.1f;
public float maxZoom = 10f;
public float minZoom = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// If the camera is orthographic...
if (GetComponent<Camera>().orthographic)
{
// ... change the orthographic size based on the change in distance between the touches.
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
}
else
{
// Otherwise change the field of view based on the change in distance between the touches.
GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 30.0f, 80.0f);
}
}
margin = new Rect(Screen.width * 0.25f, 0, Screen.width, Screen.height * 1.0f);
float count = Input.touchCount;
for (int i = 0; i < count; i++)
{
Touch tc = Input.GetTouch(i);
if(margin.Contains(tc.position))
{
x += tc.deltaPosition.x * xspeed * 0.02f;
y -= tc.deltaPosition.y * yspeed * 0.02f;
}
}
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
thanks in advance!