Search Unity

Scrolling in a limited space

Discussion in 'Scripting' started by JorobusLab, Oct 20, 2014.

  1. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Hello people, I'm wondering if you can help me with this problem(sorry for my english if bad). I've seen how to zoom in and out with two fingers( https://unity3d.com/es/learn/tutorials/modules/beginner/platform-specific/pinch-zoom ). If I zoom in, then the user must can "roam" over a space with one finger(scrolling), but until some point in that space. When he reach that point can't scroll that way, you can go the other way of course but again, until some point. The points(x+, x-, y+, y-) who limits the scrolling are those that can be seen when the camera is in its maximum zoom-out at the borders of the screen.
    So far so good, to zoom in and out(with two fingers) I should touch the "Size" attribute of the camera, and to "roam" over the scenario(with one finger) I should touch the "X" and "Y" of the transform of the camera. How can I limit the camera to stop(not scrolling towards that side) when it reachs some point? I'm lost here, thanks for your time and help.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Every time you record a movement in the x plane, add that value to a total delta. If that total delta is < your minimum distance or > your maximum, stop allowing movement.

    Do the same in the Y direction and it should be fine
     
  3. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    I understand, but how do I get that value? Is there any member, variable in Camera class which gives me(for example) the left-most side point view actually by the camera?
     
  4. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    I can't come up with a better idea, and I as well want to know if there is a better way to do this . which I believe there definitely is.

    What I would(have to since i don't know other ways) do :
    Math~~
    btw this is for orthographic camera,

    Camera cam = Camera.main;
    float height = 2f* cam.orthographicSize;
    float width = height * cam.aspect;

    with these values on hands, and some calculations
    i think you can limit the camera now.
     
  5. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    I got it thanks, I used mathematics. I was thinking also like you, that could be an easy way to see this values, if someone else does please tell us, thanks!
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, there is an easier way: use Camera.ScreenToWorldPoint to find the world position corresponding to any point on the screen (for example, the top-left corner, the bottom-right corner, etc.).

    Cheers,
    - Joe
     
  7. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Great, thank you! Another issue raise here. I've implemented "pinch to zoom" and also scrolling with one finger(it's a smartphone game). The problem is that when I zoom in, and scroll to one corner(I've limited the scrolling to some point) when I zoom out I go out of the "viewport" of my game. What I have to do is when I zoom out camera should return to its starting point(0, 0, -1), that could be easy to do, when I detect zome out just set the position of the transform in 0, 0, -1 but that is kind of "abrupt" for the user, I mean he's looking at the corner, and zoom out a bit and camera appear in middle of "world map". Do you have any advice to slowly head to the camera starting point when I zoom out?
     
  8. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    I've achieved it. I just controlled that x and y position of camera transform don't go out of certain values. Thanks all.
     
  9. coffiarts

    coffiarts

    Joined:
    Jan 7, 2013
    Posts:
    33
    Here's a quick & dirty solution, which works for me.

    Note that I am using Mathf.Clamp() to limit the values.

    HTH

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AdvancedCamera : MonoBehaviour {
    5.  
    6.     public float PanSpeed = 0.003F;
    7.     public float PinchSpeed = 0.02F;
    8.  
    9.     public float minCameraX = -200f;
    10.     public float maxCameraX = 200f;
    11.  
    12.     public float minCameraZ = -200f;
    13.     public float maxCameraZ = 200f;
    14.  
    15.     public float minCameraY = 3f;
    16.     public float maxCameraY = 50f;
    17.  
    18.     // Use this for initialization, or not. That works too.
    19.     void Start () {
    20.      
    21.     }
    22.  
    23.     // Update is called once per frame, of course.
    24.     void Update () {
    25.         // Check if we have one finger down, and if it's moved.
    26.         // You may modify this first portion to '== 1', to only allow pinching or panning at one time.
    27.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    28.             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    29.             // Translate along world cordinates. (Done this way so we can angle the camera freely.)
    30.             float newXPos = Mathf.Clamp(touchDeltaPosition.x * PanSpeed, minCameraX, maxCameraX);
    31.             float newZPos = Mathf.Clamp(touchDeltaPosition.y * PanSpeed, minCameraZ, maxCameraZ);
    32.             transform.position -= new Vector3(newXPos, 0, newZPos);
    33.         }
    34.      
    35.         // Check if we have two fingers down.
    36.         if ( Input.touchCount == 2 )
    37.         {
    38.             Touch touch1 = Input.GetTouch( 0 );
    39.             Touch touch2 = Input.GetTouch( 1 );
    40.          
    41.             // Find out how the touches have moved relative to eachother.
    42.             Vector2 curDist = touch1.position - touch2.position;
    43.             Vector2 prevDist = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition);
    44.          
    45.             float touchDelta = curDist.magnitude - prevDist.magnitude;
    46.          
    47.             // Translate along local coordinate space.
    48.             float newYPos = Mathf.Clamp(Camera.main.transform.position.y - touchDelta * PinchSpeed, minCameraY, maxCameraY);
    49.             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, newYPos, Camera.main.transform.position.z);
    50.         }
    51.     }
    52. }