Search Unity

Strange warping effect with Cinemachine Target Follow

Discussion in 'Cinemachine' started by bowserscastle, Mar 31, 2018.

  1. bowserscastle

    bowserscastle

    Joined:
    Jul 9, 2017
    Posts:
    14
    Hi there,

    I've been struggling with an issue for quite some time now and have all but run out of ideas.
    I'm using a Cinemachine Virtual Camera in a 2D project to follow around a target. The ground / background is a Unity Tilemap GameObject.
    As you can see in this gif, when following around the player (a 24x24 sprite), the background tiles seem to warp a bit. I've tried to script all types of solutions to adjust the Virtual Camera transform position and hopefully snap/move it "correctly" to no avail. I don't even know for sure that the source of the issue is with the camera setup. I'm running out of potential solutions to something that seems like a pretty straightforward and very common scenario. I've created a sample project illustrating the issue which can be downloaded here.

    Using Unity 2017.4.0f1 Personal.
    The background sprites are 32x32, with a PPU of 32. No compression, Point (no filter), rendered using a material with Shader: Sprites/Default, and Pixel snap.

    Cinemachine Virtual Cam is set to Ortho lens size 16, Body: Framing Transposer with default settings.

    Thank you so much for any suggestions or tips!!!
    It would be amazing to hear from @Adam_Myhill or @Gregoryl :)

    It feels similar to what's being described here with sub-pixel movement but I don't know for sure. The solution in that blog post feels like it should be unnecessary at this point (but again - maybe not).

    I've created camera scripts and attached them to the virtual camera as follows:

    UPDATE: This appears to be a camera size issue, as per this helpful post. Keeping this post up in case it helps others in the future.

    Code (CSharp):
    1.  
    2. float maxPixelHeight = 32;
    3. CinemachineVirtualCamera vcam;
    4.  
    5. public void Awake()
    6.     {
    7.         float scale = Screen.height / maxPixelHeight;
    8.         float orthographicSize = (Screen.height / scale) / 2f;
    9.         vcam = GetComponent<CinemachineVirtualCamera>();
    10.         vcam.m_Lens.OrthographicSize = orthographicSize;
    11. }
    12.  
    13.     public void Update()
    14.     {
    15.         Vector3 tempPos = vcam.transform.position;
    16.       Vector3 newPos = new Vector3(Mathf.RoundToInt(tempPos.x), Mathf.RoundToInt(tempPos.y), Mathf.RoundToInt(tempPos.z));
    17.         vcam.transform.position = tempPos;
    18.     }
    I've also tried:

    Code (CSharp):
    1.     public void Update()
    2.     {
    3.         Vector3 tempPos = vcam.transform.position;
    4. Vector3 newPos = new Vector3((float)System.Math.Round((decimal)tempPos.x, 2) , (float)System.Math.Round((decimal)tempPos.y, 2), (float)System.Math.Round((decimal)tempPos.z, 2));
    5.         vcam.transform.position = newPos;
    6.     }
    7.  
    and something like:
    Code (CSharp):
    1.     public void Update()
    2.     {
    3.         Vector3 tempPos = vcam.transform.position;
    4.         vcam.transform.position = new Vector3(RoundToNearestPixel(tempPos.x), RoundToNearestPixel(tempPos.y), RoundToNearestPixel(tempPos.z));
    5.     }
    6.  
    7.     public float RoundToNearestPixel(float unityUnits)
    8.     {
    9.         float valueInPixels = unityUnits * maxPixelHeight;
    10.         valueInPixels = Mathf.Round(valueInPixels);
    11.         float roundedUnityUnits = valueInPixels * (1 / maxPixelHeight);
    12.         return roundedUnityUnits;
    13.     }
     
    Last edited: Apr 3, 2018
    Gregoryl likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    @bowserscastle Sounds like you've got this solved. Let me know if there's anything else that I can help with.
     
    bowserscastle likes this.
  3. bowserscastle

    bowserscastle

    Joined:
    Jul 9, 2017
    Posts:
    14
    Thank you @Gregoryl! Yep, all is well with the shimmer effect. I'm seeing a different issue though with 2D camera jitters as detailed here. I spent a lot of time going through a lot of threads where others have seen similar issues, and produced a super stripped-down project demonstrating the issue. I'm sure it's something very simple that I'm overlooking. It would be of tremendous help if you had a few moments to take a look. Thank you!