Search Unity

Resolved Camera transform function doesn't work since upgrading to 2020.2

Discussion in 'Scripting' started by robdood, Jan 18, 2021.

  1. robdood

    robdood

    Joined:
    Jan 18, 2021
    Posts:
    14
    Hello all, newbie at Unity and these forums, hoping someone can help!

    So yesterday this code worked just fine - currentMousePos is updated while a mousebutton is held down:

    Code (CSharp):
    1. public void PanCamera()
    2.     {
    3.         currentMousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    4.         mouseCamAdjust = currentMousePos.x - startMousePos.x;
    5.  
    6.         Debug.Log("start:" + startMousePos + "current:" + currentMousePos + "adjust:" + mouseCamAdjust);
    7.  
    8.         //translate cam, but only on x axis
    9.         //multiply by 2 to speed up the scrolling so you dont have to move mouse too much
    10.         cam.transform.position += new Vector3(mouseCamAdjust * 2, 0f, 0f);
    11.     }
    Today I upgraded from 2019.4.17f1 to 2020.2.1f1 and for some reason the panning seems to be broken. I've not changed any code. The debug string above is appearing in my console, so we're entering this function, it just seems to not 'notice' any difference between the start position and current position - i.e. they always seem to be the same.

    Even more bizarrely, while debugging and leaving the game running in play mode, if I edit the script and save, sometimes it just suddenly starts to work. Very strange!

    I guess it could be because it's running so quick that the start/current mouse pos end up being the same, but it's unusual that it worked yesterday but now doesn't.

    Anyhoo - any pointers on what might have happened, or an alternative way to achieve the same result would be appreciated.
     
  2. robdood

    robdood

    Joined:
    Jan 18, 2021
    Posts:
    14
    Update - worked around it by using an IEnumerator with a miniscule wait. If the wait was too long (0.01) the movement appeared jumpy/stuttery. Bizarre, but it worked!

    Code (CSharp):
    1.     IEnumerator GetCurrentmouse()
    2.     {
    3.         yield return new WaitForSeconds(0.0001f);
    4.         currentMousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    5.     }
    Called it as first step of the PanCamera function.

    Code (CSharp):
    1.     public void PanCamera()
    2.     {
    3.         //get current mouse pos, use coroutine since otherwise things run so fast it both curr and start positions end up identical!
    4.         StartCoroutine(GetCurrentmouse());
    5.  
    6.         //calculate amount by which to adjust the camera x value
    7.         mouseCamAdjust = startMousePos.x - currentMousePos.x;
    8.  
    9.         //translate cam, but only on x axis
    10.         //multiply to speed up the scrolling so you dont have to move mouse too much
    11.         cam.transform.position += new Vector3(mouseCamAdjust * 1.5f, 0f, 0f);
    12.  
    13.     }