2016-09-21 12 views
6

Używam mapy większej niż wyświetlany przeze mnie ekran. Dlatego muszę mieć możliwość poruszania się po tej mapie. Mam problem z zamocowaniem kamery i mapy. Chcę móc korzystać z rozbieżności obrazu jako szerokości i wysokości zacisku. Problemem są jednostki.Jak ustawić granicę mapy i panoramowanie tej mapy tak samo?

Zdjęcie ma 2144 x 1708 Transpozycja kamery składa się z pojedynczych cyfr (14 x 7) lub czegoś w tym stylu.

Cały kod, którego używam, znajduje się poniżej.

private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts 
private bool isPanning;  // Is the camera being panned? 

public bool useBoundary = true; 
public Vector2 boundaryMin; 
public Vector2 boundaryMax; 

public Image map; 


void Start() 
{ 

    Camera cam = Camera.main; 

    float mapRatio = map.rectTransform.rect.width/map.rectTransform.rect.height; 

    float mapScreenHeight = (1.5f * cam.orthographicSize); 
    float mapScreenWidth = (3f * mapScreenHeight) * cam.aspect; 

    boundaryMin = new Vector2(0, 1); 
    boundaryMax = new Vector2(map.rectTransform.rect.width, map.rectTransform.rect.height); 



} 


void Update() 
{ 
    // Get the left mouse button 
    if (Input.GetMouseButtonDown(0)) 
    { 
     // Get mouse origin 
     mouseOrigin = Input.mousePosition; 
     isPanning = true; 
    } 


    // Disable movements on button release 
    if (!Input.GetMouseButton(0)) isPanning = false; 

    // Rotate camera along X and Y axis 

    // Move the camera on it's XY plane 
    if (isPanning) 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 
     Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
     transform.Translate(move, Space.Self); 
     BoundaryCheck(); 
    } 

} 

void BoundaryCheck() 
{ 
    if (!useBoundary) 
     return; 

    Vector3 newPos = transform.position; 

    newPos.x = Mathf.Clamp(newPos.x, boundaryMin.x, boundaryMax.x); 
    newPos.y = Mathf.Clamp(newPos.y, boundaryMin.y, boundaryMax.y); 
    transform.position = newPos; 
} 

}

Każda pomoc będzie mile widziana.

+0

Nie możesz użyć innego zdjęcia o rozmiarze 14x7? I wystawiasz 'boundaryMin' &' boundaryMax' w edytorze, ale potem w 'Start()' nadpisujesz ich wartość, więc jaki jest sens ich upubliczniania? –

+0

Mogę zmienić rzeczy na prywatne, to nie jest problem. Na mapie panoramowania zwykle ustawiasz rozmiar mapy na podstawie ekranu? Jak to działa, gdy ekran zmienia się na urządzeniu mobilnym? –

Odpowiedz

2

Możesz to zrobić za pomocą Unity UI - Scroll Rect.

Wyjazd Unity UI - Scroll Rect - Introduction

+0

Wygląda na to, że może działać! Pozwól mi odejść. –

+0

Pewnie. Wykonaj upvote, jeśli uważasz, że odpowiedź jest pomocna. –

1

Moim zdaniem powinna trzeba skorzystać z poniższego skryptu do kamery przesuwanie, powiększanie i obracanie. Możesz wyłączyć każdą niepożądaną funkcję. Teraz zgodnie z Twoim pytaniem musisz ograniczyć ruch (zgodnie z granicą obrazu). po prostu można ograniczyć ruchy kamery na osi różnicowej zgodnie z własnymi ograniczeniami. Powinieneś umieszczać kostki w granicach mapy i używać ich pozycji jako limitu ruchu kamery i panoramowania itp.

using UnityEngine; 
using System.Collections; 

public class CamMovementManager : MonoBehaviour 
{ 
    #region Vars 
    public float turnSpeed = 1.0f;  // Speed of camera turning when mouse moves in along an axis 
    public float panSpeed = 4.0f;  // Speed of the camera when being panned 
    public float zoomSpeed = 4.0f;  // Speed of the camera going back and forth 

    private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts 
    private bool isPanning;  // Is the camera being panned? 
    private bool isRotating; // Is the camera being rotated? 
    private bool isZooming;  // Is the camera zooming? 

    private float pannPosLimit = 300f; 
    private int fovMin = 15; 
    private int fovMax = 90; 

    #endregion Vars 

    #region UnityEvents 

    void Update() 
    { 
     // Get the left mouse button 
     if (Input.GetMouseButtonDown(0)) 
     { 
      // Get mouse origin 
      mouseOrigin = Input.mousePosition; 
      isRotating = true; 
     } 

     // Get the right mouse button 
     if (Input.GetMouseButtonDown(1)) 
     { 
      // Get mouse origin 
      mouseOrigin = Input.mousePosition; 
      isPanning = true; 
     } 

     // Get the middle mouse button 
     if (Input.GetMouseButtonDown(2)) 
     { 
      // Get mouse origin 
      //mouseOrigin = Input.mousePosition; 
      //isZooming = true; 
     } 

     //changing fov on mouse scroll to zoomIn/out 
     float fov = Camera.main.fieldOfView; 
     fov += Input.GetAxis("Mouse ScrollWheel") * 10f; 
     fov = Mathf.Clamp(fov, fovMin, fovMax); 
     Camera.main.fieldOfView = fov;//*/ 


     // Disable movements on button release 
     if (!Input.GetMouseButton(0)) isRotating = false; 
     if (!Input.GetMouseButton(1)) isPanning = false; 
     if (!Input.GetMouseButton(2)) isZooming = false; 

     // Rotate camera along X and Y axis 
     if (isRotating) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 
      //Debug.Log("rotate pos : " + pos); 
      transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed); 
      transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed); 

     } 

     // Move the camera on it's XY plane 
     if (isPanning) 
     { 
      if (Input.mousePosition.y > pannPosLimit) 
      { 
       Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

       Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
       transform.Translate(move, Space.Self); 
      } 
     } 

     // Move the camera linearly along Z axis 
     if (isZooming) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

      Vector3 move = pos.y * zoomSpeed * transform.forward; 
      transform.Translate(move, Space.World); 
     } 
    } 

    #endregion 

    #region CustomMethods 

    public void CamRotating() 
    { 

     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed); 
     transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed); 
    } 

    public void CamPanning() 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
     transform.Translate(move, Space.Self); 
    } 

    public void CamZooming() 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     Vector3 move = pos.y * zoomSpeed * transform.forward; 
     transform.Translate(move, Space.World); 
    } 

    #endregion CustomMethods 
} 
+0

Właściwie zacząłem od kodu podobnego do tego, co napisałeś. Spróbuję użyć metody pudełkowej i zobaczę, czy to się dobrze zgadza z rozmiarem ekranu. –

+0

pudełka są ograniczeniami dla ruchu kamery, jak powiedziałeś o granicy obrazu –

+0

pozycja pudełka są tylko używane, więc możesz wyjąć z siatki skrzynki –