Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Zooming & Panning

Iniciado por Paco Morales, Octubre 08, 2015, 04:21:50 PM

Tema anterior - Siguiente tema
Hola,Estoy tratando de hacer un panning o arrastrar la camara con dos dedos, pues ya esta configurado el zoom juntando los 2 dedos. Lo que quiero, es con los 2 dedos sobre la pantalla arrastrar la cámara para hacer el panning. Conseguí este script que me hace el zoom bien pero no me hace el panning.Alguna sugerencia?
#pragma strict

public var perspectiveZoomSpeed : float = 0.5f;        // The rate of change of the field of view in perspective mode.
public var orthoZoomSpeed : float = 0.5f;        // The rate of change of the orthographic size in orthographic mode.


function Update()
{
    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {

        // Store both touches.
        var touchZero = Input.GetTouch(0);
        var touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        var touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        var prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        var touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in the distances between each frame.
        var 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, 0.1f, 179.9f);
        }
    }
}
 

Prueba el asset TouchScript. Acepta Gestures de cualquier tipo, incluido el de zoom con dos dedos.Es gratis y lo tengo probado en la 5.2

Ese script me suena bastante jaja // Find the position in the previous frame of each touch.
    var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    var touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

// Saca las direcciones que tiene cada dedo
    var direccion0 = touchZero.position - touchZeroPrevPos;
    var direccion1 = touchOne.position - touchOnePrevPos;

// Verifica si siguen la misma direccion
    if(Vector2.Dot(direccion0, direccion1) > 0.8f)
    //Mueve la camara según la dirección 1 o 2Claro que de esta forma verificas cada frame que movimiento vas a hacer y sería muy molesto que una vez empieces a mover la cámara ésta haga zoom y viceversa...

[quote author=iRobb" data-ipsquote-contapp="forums" data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentid="29483" data-ipsquote-contentcommentid="117101 date=1444319667]Prueba el asset TouchScript. Acepta Gestures de cualquier tipo, incluido el de zoom con dos dedos.Es gratis y lo tengo probado en la 5.2[/quote]Hola iRobb, pues ya instale el TouchScript, pero no he encontrado como hacer el pan y zoom con 2 dedos.Gracias
[quote author=juanma_teso" data-ipsquote-contapp="forums" data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentid="29483" data-ipsquote-contentcommentid="117165">Ese script me suena bastante jaja // Find the position in the previous frame of each touch.var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    var touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

// Saca las direcciones que tiene cada dedo
    var direccion0 = touchZero.position - touchZeroPrevPos;
    var direccion1 = touchOne.position - touchOnePrevPos;

// Verifica si siguen la misma direccion
    if(Vector2.Dot(direccion0, direccion1) > 0.8f)
    //Mueve la camara según la dirección 1 o 2Claro que de esta forma verificas cada frame que movimiento vas a hacer y sería muy molesto que una vez empieces a mover la cámara ésta haga zoom y viceversa...[/quote]Hola Juanma_teso, Si ese script esta en la sección de tutoriales de Unity  ....Voy a tratar de implementar esto que escribiste. Deje hace 2 años Unity....Y acabo de regresar hace 3 dias...Si hubiera seguido, ya no tendria tantos problemas....Esto de programar es un deporte de alto rendimiento, no lo puedes dejar....

Hola,Al final le puse un limite al zooming... Como ven este script?  le agregue un  limite, pero no se si hice una barbaridad o le puse algo de mas....Ahora funciona bien.Cualquier ayuda es muy agradecida. 
#pragma strict

public var perspectiveZoomSpeed : float = 0.5f;        // The rate of change of the field of view in perspective mode.
public var orthoZoomSpeed : float = 0.5f;        // The rate of change of the orthographic size in orthographic mode.
var minZoom : float = 1.0;
var maxZoom : float = 20.0;
var mapWidth : float = 40.0;
var mapHeight : float = 40.0;
var _camera : Camera;

function Start ()
{
    _camera = Camera.main;
    maxZoom = 0.5 * (mapWidth / _camera.aspect);

    if(mapWidth > mapHeight)
    {
        maxZoom = 0.5 * mapHeight;
    }
}

function Update()
{
    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {
        // Store both touches.
        var touchZero = Input.GetTouch(0);
        var touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        var touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        var prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        var touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in the distances between each frame.
        var 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);
            _camera.orthographicSize = Mathf.Clamp(_camera.orthographicSize, minZoom, maxZoom) - 0.001;
        }
        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, 0.1f, 179.9f);
        }
    }
}
 

Etiquetas: