Search Unity

Panning Camera

Discussion in 'Scripting' started by mrsql, Aug 28, 2014.

  1. mrsql

    mrsql

    Joined:
    Aug 28, 2014
    Posts:
    1
    I am new to Unity. I have created some basic camera framework to move the camera around on a top down type view and it does the job but why does the mouse panning seem so smooth but the key moving(w,s,d,a) seem choppy?

    Code (CSharp):
    1.         // panning    
    2.         if (Input.GetMouseButton(0))
    3.         {
    4.             transform.Translate(Vector3.right * Time.deltaTime * PanSpeed * (Input.mousePosition.x - Screen.width * 0.5f) / (Screen.width * 0.5f), Space.World);
    5.             transform.Translate(Vector3.forward * Time.deltaTime * PanSpeed * (Input.mousePosition.y - Screen.height * 0.5f) / (Screen.height * 0.5f), Space.World);
    6.         }
    7.         else
    8.         {
    9.             if (Input.anyKey)
    10.             {
    11.                 string keyPressed = Input.inputString;
    12.                 switch (keyPressed)
    13.                 {
    14.                     case "d":
    15.                         transform.Translate(Vector3.right * Time.deltaTime * PanSpeed, Space.Self);
    16.                         break;
    17.                     case "a":
    18.                         transform.Translate(Vector3.left * Time.deltaTime * PanSpeed, Space.Self);
    19.                         break;
    20.                     case "s":
    21.                         transform.Translate(Vector3.down * Time.deltaTime * PanSpeed, Space.Self);
    22.                         break;
    23.                     case "w":
    24.                         transform.Translate(Vector3.up * Time.deltaTime * PanSpeed, Space.Self);
    25.                         break;
    26.                     default:
    27.                         break;
    28.  
    29.                 }
    30.             }
    31.         }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First suggestion would be to use Input.GetAxis rather than what you're doing there. It makes cleaner, easier maintained code, and it also has smoothing and such. This may be the source of your choppiness.