I have a simple two-parter today I am requesting help with …
I have this script that changes camera position on a horizontal plane when a user uses three fingers but I need it smoother. It needs to Lerp I think, but I can’t do it with Translate, can I? How can I make this movement smoother?
if (Input.touchCount == 3 Input.GetTouch(0).phase)
{
// Get movement of the fingers since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across plane
editcamera.Translate (touchDeltaPosition.x * cameraspeed * Time.deltaTime, touchDeltaPosition.y * cameraspeed * Time.deltaTime, 0);
}
I would like to control the camera.orthographicSize with a two-fingered pinch-zoom and can not seem to be able to do it? I know camera.orthographicSize is not fixed, but for the life of me can’t get it to change on the fly. How do I code this sort of effect; i.e.,
if (Input.touchCount == 2)
{
// Get movement of the fingers since last frame, pinching or zoom?
// fingers positions get closer = zoom in, fingers spreading apart = zoom out
// if zoom in to no more than camera.orthographicSize = 5
// or zoom out to camera.orthographicSize <= 30
}
If you look at the SmoothFollow script in the wiki it has a lerp example which is useful for smooth transformations. That said however my smooth follow seams to be broke after upgrading to 3.2. It jitters pretty bad now.
Smoothfollow2 seems to want the camera it’s attached to to follow an object (a Transform on screen) as it moves in the scene and it’s referring to 3D. I need to camera to smoothly follow and Lerp to a user’s touch location.
I have the zoom pinch almost working … but how do I make this …
Here’s the pinch zoom code I’m working with, except it only seems to recognize zoom out movement, even if I touch and then pinch in it says I’m pinching out …
// set limits to camera's orthographic size
var minCamOrthoSize: float = 5.0;
var maxCamOrthoSize: float = 65.0
var smoothTime: float = 3.5;
function Update () {
// zoom camera in and out with two fingers on screen
// orthographic size default = 30
var currentOrthoSize = camera.orthographicSize;
var smoothZoomOut = Mathf.Lerp(currentOrthoSize, maxCamOrthoSize, Time.deltaTime * smoothTime);
var smoothZoomIn = Mathf.Lerp(currentOrthoSize, minCamOrthoSize, Time.deltaTime * smoothTime);
// tempStatus.text = "" + currentOrthoSize;
var lastf0f1Dist: float;
if (Input.touchCount == 2)
{
// fingers data
f0 = Input.GetTouch(0);
f1 = Input.GetTouch(1);
// fingers positions
f0Pos = new Vector3(f0.position.x, f0.position.y, 0);
f1Pos = new Vector3(f1.position.x, f1.position.y, 0);
// fingers movements
f0Delta = new Vector3(f0.deltaPosition.x, f0.deltaPosition.y, 0);
f1Delta = new Vector3(f1.deltaPosition.x, f1.deltaPosition.y, 0);
// fingers distance
f0f1Dist = Vector3.Distance(f0.position, f1.position);
// if both fingers moving
if (f0.phase == TouchPhase.Moved f1.phase == TouchPhase.Moved)
{
// fingers moving direction
f0Dir = f0Delta.normalized;
f1Dir = f1Delta.normalized;
// dot product of directions
dot = Vector3.Dot(f0Dir, f1Dir);
// if fingers moving in opposite directions
if (dot < -0.7f)
{
pinchDelta = f0f1Dist - lastf0f1Dist;
// if fingers move more than a threshold
if (Mathf.Abs(pinchDelta) > 2)
{
// if pinch out, zoom out
if (f0f1Dist > lastf0f1Dist)
{
tempStatus.text = "zoom out = " + currentOrthoSize;
}
// if pinch in, zoom in
else if (f0f1Dist < lastf0f1Dist)
{
tempStatus.text = "zoom in = " + currentOrthoSize;
}
}
}
}
// record last distance, for delta distances
lastf0f1Dist = f0f1Dist;
}
}
Any idea why this only sees me as zooming out?
Later edit … probably has something to do with the fact that I am looking down at the X-Z axis with my ortho camera. I am working on it.
Just stumbled across this lonely and aging thread… below is the solution for zooming. DaveJJ, I noticed that you had a scoping issue with your “lastf0f1Dist” variable. Would love to know if you found a better solution as well.
Regards,
Chris
private var lastf0f1Dist: float = 0.0;
function Update ()
{
if (Input.touchCount == 2) { //zoom feature
var f0 : Touch = Input.GetTouch(0);
var f1 : Touch = Input.GetTouch(1);
// fingers positions
var f0Pos : Vector2 = Vector2(f0.position.x, f0.position.y);
var f1Pos : Vector2 = Vector2(f1.position.x, f1.position.y);
// fingers movements
var f0Delta : Vector2 = Vector2(f0.deltaPosition.x, f0.deltaPosition.y);
var f1Delta : Vector2 = Vector2(f1.deltaPosition.x, f1.deltaPosition.y);
// fingers distance
var f0f1Dist : float = Vector2.Distance(f0.position, f1.position);
// if both fingers moving
if (f0.phase == TouchPhase.Moved f1.phase == TouchPhase.Moved) {
// fingers moving direction
var f0Dir : Vector2 = f0Delta.normalized;
var f1Dir : Vector2 = f1Delta.normalized;
// dot product of directions
var dot : float = Vector2.Dot(f0Dir, f1Dir);
// if fingers moving in opposite directions
if (dot < -0.7f) {
pinchDelta = f0f1Dist - lastf0f1Dist;
// if fingers move more than a threshold
if (Mathf.Abs(pinchDelta) > 2) {
if (f0f1Dist > lastf0f1Dist) { //zoom out
Debug.Log("zoom out " + f0f1Dist);
} else if (f0f1Dist < lastf0f1Dist) { // zoom in
Debug.Log("zoom in " + f0f1Dist);
}
}
}
}
// record last distance, for delta distances
lastf0f1Dist = f0f1Dist;
} //end touchCount == 2
}
public class CameraZoom : MonoBehaviour {
private float startTouchMagnitude;
private float startTouchZoom;
private float targetZoom;
public float minZoom = 2.0f;
public float maxZoom = 5.0f;
pubic float zoomSpeed = 0.2f;
void Update() {
if(Input.touchCount == 2) { // only check when 2 fingers are pressed
if(Input.touches[1].phase==TouchPhase.Began) { // check that the second finger just began touching
// remember the distance between the two fingers
startTouchMagnitude = (Input.touches[0].position-Input.touches[1].position).magnitude;
// remember the current zoom/orthogrphic size of the camera, we use this to interpolate the zoom smoothly
startTouchZoom = Camera.main.orthographicSize;
}
// calculate the relative change since last frame
float relativeMagnitudeChange = startTouchMagnitude / (Input.touches[0].position-Input.touches[1].position).magnitude;
targetZoom = startTouchZoom * relativeMagnitudeChange;
}
// Limit the zoom between min and max
targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);
}
// smoothly zoom in and out
Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, targetZoom, zoomSpeed);
}
Basically all your need is the: magnitude between two fingers when they first begun and the current magnitude. Then you can calculate the zoom relatively.
i.e. when you first touch the display with two fingers, the magnitude may be 2.0f. When you move your fingers away the new magnitude is 4.0f. Then you get 2.0f/4.0f = 0.5f.
If the camera was at zoom 2.0f, it would be at 1.0f then (moving fingers away = zoom in). Moving fingers closer together = zoom out.
You will also notice that this works quite nicely, if you zoom out like this your fingers will always be on the objects you touched at the beginning (unless you hit min/max Zoom before)