Search Unity

SmoothDamp on Camera Movement moving faster in X.

Discussion in 'Scripting' started by fwalker, Jan 22, 2014.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I know I am missing something seriously silly here but I cannot figure it out. Could a kind soul put me out of my misery, please?
    The following code works great as long as the call to BindCamera() is not made. If I try to limit the camera movement to the world edges, suddenly the camera moves faster in X than in Y when scrolling back to the target. Why in the world would that happen?

    Code (csharp):
    1.  
    2.     // Update is called once per frame
    3.     void Update ()
    4.     {
    5.        UpdateCameraPosition();
    6.        BindCamera();
    7.        SetCameraPosition();
    8.     }
    9.            
    10.     private void UpdateCameraPosition()
    11.     {
    12.         //If I am scrolling just move the camera according to the mouse input
    13.         if( m_scrolling )
    14.         {
    15.            Vector3 inputSlideAmount = m_prevMousePosition - Input.mousePosition;
    16.            m_scrollAmount = new Vector3(
    17.                     inputSlideAmount.x * m_scrollRatio.x * parallaxRation,
    18.                     inputSlideAmount.y * m_scrollRatio.y * parallaxRation, 0 );      
    19.    
    20.            m_cameraPosition = m_cameraPreScroll + m_scrollAmount;
    21.            m_cameraVelocity = Vector3.zero;
    22.         }
    23.         else
    24.         {
    25.            //If I am not scrolling and there is a target for my camera
    26.            //then scroll the camera back to center on the target
    27.             if( m_target !=  null )
    28.             {
    29.                 m_cameraPosition = Vector3.SmoothDamp(
    30.                          m_cameraPosition,  
    31.                          m_target.transform.position,
    32.                          ref m_cameraVelocity,
    33.                          m_dampingFactor );
    34.             }
    35.         }
    36.     }
    37.            
    38.     private void SetCameraPosition()
    39.     {
    40.         transform.localPosition =
    41.                     new Vector3( m_cameraPosition.x,
    42.                                  m_cameraPosition.y,
    43.                                  transform.position.z);
    44.     }
    45.            
    46.     //Do not allow the camera to go outside the world boundaries
    47.     private void BindCamera()
    48.     {
    49.         m_cameraPosition =
    50.         new Vector3(
    51.              Mathf.Clamp(m_cameraPosition.x, m_minCameraPosition.x,  m_maxCameraPosition.x ),
    52.              Mathf.Clamp(m_cameraPosition.y, m_minCameraPosition.y, m_maxCameraPosition.y ),
    53.                  m_cameraPosition.z);
    54.     }
    55.  
    56.