Hello everybody!
I am trying to code a fingergesture: pinching for camera zoom.
It should do following:
- When beginning with the two finger, the cam shpould stay with its original FOV
- when moving finger apart, the cam should zoom out, when moving together, it should zoom in
- when the fingers leave the screen, the zoom should stay
- when touching again, the new zoom is the start of the new and same procedure.
So, it's a simple zoom ... but the cam jumps just in and out with my code:
if (Input.touchCount == 2 && Input.GetTouch(1).phase == TouchPhase.Began){
var pinchLength : float = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
}
if(Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)){
var deltaLength : float = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
deltaLength = pinchLength - deltaLength;
deltaLength = deltaLength /90;
camActive.gameObject.GetComponent(camAutoZoom).pinchStrength = deltaLength;
}
if(Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Ended)){
camActive.gameObject.GetComponent(camAutoZoom).actualZoom += deltaLength;
camActive.gameObject.GetComponent(camAutoZoom).pinchStrength = 0;
deltaLength = 0;
}
And now put together in camAutoZoom-Script with this simple line:
gameObject.camera.fov = originalFOV + actualZoom + pinchStrength;
asked
Aug 29 '12 at 12:26 AM
RickTick
41
●
7
●
23
●
26