Noticias

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

Problemas con Camara

Iniciado por Sitorxx, Diciembre 01, 2015, 01:15:38 AM

Tema anterior - Siguiente tema
Buenas, estoy intentando crear un juego con una proyección en isométrica, el problema esta en el movimiento de la cámara, al exportar a Android se comporta de una forma muy extraña, este es el script que uso para el movimiento de cámara en Android:  
public float moveSensitivityX = 1.0f;
    public float moveSensitivityY = 1.0f;
    public bool updateZoomSensitivity = true;
    public float orthoZoomSpeed = 0.05f;
    public float minZoom = 1.0f;
    public float maxZoom = 20.0f;
    public bool invertMoveX = false;
    public bool invertMoveY = false;
    public float mapWidth = 60.0f;
    public float mapHeight = 40.0f;
   
    public float inertiaDuration = 1.0f;
   
    private Camera _camera;

    private float minX, maxX, minY, maxY;
    private float horizontalExtent, verticalExtent;
   
    private float scrollVelocity = 0.0f;
    private float timeTouchPhaseEnded;
    private Vector2 scrollDirection = Vector2.zero;

    void Start ()
    {
        _camera = Camera.main;

        maxZoom = 0.5f * (mapWidth / _camera.aspect);

        if (mapWidth > mapHeight)
            maxZoom = 0.5f * mapHeight;

        if (_camera.orthographicSize > maxZoom)
            _camera.orthographicSize = maxZoom;

        CalculateLevelBounds ();
    }
   
    void Update ()
    {
        if (updateZoomSensitivity)
        {
            moveSensitivityX = _camera.orthographicSize / 5.0f;
            moveSensitivityY = _camera.orthographicSize / 5.0f;
        }

        Touch[] touches = Input.touches;

        if (touches.Length < 1)
        {
            //if the camera is currently scrolling
            if (scrollVelocity != 0.0f)
            {
                //slow down over time
                float t = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
                float frameVelocity = Mathf.Lerp (scrollVelocity, 0.0f, t);
                _camera.transform.position += -(Vector3)scrollDirection.normalized * (frameVelocity * 0.05f) * Time.deltaTime;
               
                if (t >= 1.0f)
                    scrollVelocity = 0.0f;
            }
        }

        if (touches.Length > 0)
        {
            //Single touch (move)
            if (touches.Length == 1)
            {
                if (touches[0].phase == TouchPhase.Began)
                {
                    scrollVelocity = 0.0f;
                }
                else if (touches[0].phase == TouchPhase.Moved)
                {
                    Vector2 delta = touches[0].deltaPosition;

                    float positionX = delta.x * moveSensitivityX * Time.deltaTime;
                    positionX = invertMoveX ? positionX : positionX * -1;

                    float positionY = delta.y * moveSensitivityY * Time.deltaTime;
                    positionY = invertMoveY ? positionY : positionY * -1;

                    _camera.transform.position += new Vector3 (positionX, positionY, 0);

                    scrollDirection = touches[0].deltaPosition.normalized;
                    scrollVelocity = touches[0].deltaPosition.magnitude / touches[0].deltaTime;


                    if (scrollVelocity <= 100)
                        scrollVelocity = 0;
                }
                else if (touches[0].phase == TouchPhase.Ended)
                {
                    timeTouchPhaseEnded = Time.time;
                }
            }


            //Double touch (zoom)
            if (touches.Length == 2)
            {
                Vector2 cameraViewsize = new Vector2 (_camera.pixelWidth, _camera.pixelHeight);

                Touch touchOne = touches[0];
                Touch touchTwo = touches[1];

                Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
                Vector2 touchTwoPrevPos = touchTwo.position - touchTwo.deltaPosition;

                float prevTouchDeltaMag = (touchOnePrevPos - touchTwoPrevPos).magnitude;
                float touchDeltaMag = (touchOne.position - touchTwo.position).magnitude;

                float deltaMagDiff = prevTouchDeltaMag - touchDeltaMag;

                _camera.transform.position += _camera.transform.TransformDirection ((touchOnePrevPos + touchTwoPrevPos - cameraViewsize) * _camera.orthographicSize / cameraViewsize.y);

                _camera.orthographicSize += deltaMagDiff * orthoZoomSpeed;
                _camera.orthographicSize = Mathf.Clamp (_camera.orthographicSize, minZoom, maxZoom) - 0.001f;

                _camera.transform.position -= _camera.transform.TransformDirection ((touchOne.position + touchTwo.position - cameraViewsize) * _camera.orthographicSize / cameraViewsize.y);

                CalculateLevelBounds ();
            }
        }
    }

    void CalculateLevelBounds ()
    {
        verticalExtent = _camera.orthographicSize;
        horizontalExtent = _camera.orthographicSize * Screen.width / Screen.height;
        minX = horizontalExtent - mapWidth / 2.0f;
        maxX = mapWidth / 2.0f - horizontalExtent;
        minY = verticalExtent - mapHeight / 2.0f;
        maxY = mapHeight / 2.0f - verticalExtent;
    }

    void LateUpdate ()
    {
        Vector3 limitedCameraPosition = _camera.transform.position;
        limitedCameraPosition.x = Mathf.Clamp (limitedCameraPosition.x, minX, maxX);
        limitedCameraPosition.y = Mathf.Clamp (limitedCameraPosition.y, minY, maxY);
        _camera.transform.position = limitedCameraPosition;
    }

    void OnDrawGizmos ()
    {
        Gizmos.DrawWireCube (Vector3.zero, new Vector3 (mapWidth, mapHeight, 0));
    }
}
 El problema es que el tipo de cámara que utilizo es Ortographic, no se si eso puede influir, pero básicamente cuando le meto zoom, los objetos desaparecen, si quito el zoom empieza a temblar la escena, y cuando lo muevo varias veces lateralmente desaparece todo. Alguna idea?  Gracias

cuando hice la cámara para las fichas en Elementaria noté que se debe ajustar el plano final y el inicial de la cámara para que los objetos no desaparezcan. Si el zoom los desaparece tienes que alejarlos más, es como una limitación de la cámara ortográfica. Por ejemplo en mi caso los planos se separan 10 unidades y centro las esferas a 5 unidades. Además la distancia de la cámara cambiaba los reflejos especulares de los shaders a pesar de ser ortográfica, lo que fue una sorpresa... pero bueno de todas maneras cambié a sprites para optimizar...

Vale, probaré lo de alejarlos, pero respecto al tema de que la cámara tiemble tenéis alguna idea de porque puede ser?


public class IsoObject : MonoBehaviour {

public static readonly Matrix4x4 projectionMatrix = Matrix4x4.identity;

// you may vary this, but keep > 0.1f
public const float depthScaling = 1f ;

static IsoObject() {

   //Isometric projection matrix
   projectionMatrix.m00 = 1;
   projectionMatrix.m01 = -1;
   projectionMatrix.m02 = 0;
   projectionMatrix.m10 = 1/2f;
   projectionMatrix.m11 = 1/2f;
   projectionMatrix.m12 = 1;
   projectionMatrix.m20 = depthScaling;
   projectionMatrix.m21 = depthScaling;
   projectionMatrix.m22 = -depthScaling;

}

//Position in virtual 3d space, not the Isometric projection (which is a Vector2 and stored in transform.position)
[SerializeField]
private Vector3 position;

[SerializeField]
private Vector3 size = new Vector3(1, 1, 1);

//flag to display bounds in the editor
public bool displayBounds = true;

//Size property
public Vector3 Size {
   get { return size; }
   set {
      size = value;

   }
}

//the position of the object, not the isometric projection
public Vector3 Position {
   get { return position; }
   set {
      position = value;
      updateIsoProjection();
   }
}

/// <summary>
/// current depth/sortingOrder.
/// </summary>
public float Depth {
   get {
      return transform.position.z;
   }
   private set {
      transform.position = new Vector3(transform.position.x, transform.position.y, value);
   }
}

void Start() {
   updateIsoProjection();
}

/// <summary>
/// updates the isometric projection of this isoObject
/// </summary>
protected void updateIsoProjection() {
   transform.position = isoProjection(Position);
}

/// <summary>
/// Isometric Projection of an object in 3d Space.
/// </summary>
/// <param name="pt"></param> The Object in virtual 3d space
/// <returns></returns>
private Vector3 isoProjection(Vector3 pt) {
   return projectionMatrix.MultiplyVector(pt);
}

}
  Este es el código que estoy usando para poder hacer bien la isométrica, es del toolkit de Unity. Puede que este choque?

Etiquetas: