Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Grab and Drag Camera Style

Discussion in 'Scripting' started by Grimshad, Nov 24, 2012.

  1. Grimshad

    Grimshad

    Joined:
    Dec 28, 2011
    Posts:
    9
    Does anyone know of a tutorial that will help me make a grab and drag style camera? I want to be able to click and drag on the surface of the game world to move the camera around on the x-y axis (not up and down). I have written this script for the camera and it works, but it's really half assed since i don't really know how to make exactly what i want.

    P.S. The reason I am changing Y to Z is because Input.mousePosition returns Y as up and down and for some stupid reason transform.position uses Z as up and down. Why can't everything just use Z?

    Code (csharp):
    1.  
    2. var HasClicked = false;
    3. var ClickedPosition = Vector3(0,0,0);
    4.  
    5. function Start ()
    6. {
    7.    
    8. }
    9.  
    10. function Update ()
    11. {
    12.     if (Input.GetMouseButton(0))
    13.     {
    14.         if (HasClicked == false)
    15.         {
    16.             ClickedPosition = Input.mousePosition;
    17.             HasClicked = true;
    18.         }
    19.  
    20.         var NewVec = ClickedPosition - Input.mousePosition;
    21.         NewVec.z = NewVec.y;
    22.         NewVec.y = 20;
    23.  
    24.         transform.position = NewVec;
    25.     }
    26.     else
    27.     {
    28.         HasClicked = false;
    29.     }
    30. }
    Thanks
     
    Last edited: Nov 24, 2012
  2. lucifer1101

    lucifer1101

    Joined:
    Sep 24, 2011
    Posts:
    7
    I came accross a script that included some 3ds max like controls which included panning plus others. I have just done a quick search for it now but it's somewhere I can't remember so in the meantime you may find something on this page useful to your question.

    http://answers.unity3d.com/questions/26079/left-click-and-pan.html

    EDIT: You should be able to find it here http://wiki.unity3d.com/index.php/MouseOrbitZoom

    This is the bit I was talking about
    Code (csharp):
    1.  
    2. // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
    3. else if (Input.GetMouseButton(2))
    4. {
    5.     //grab the rotation of the camera so we can move in a psuedo local XY space
    6.     target.rotation = transform.rotation;
    7.     target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
    8.     target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
    9. }
    10.  
     
    Last edited: Nov 24, 2012