Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LookAt in 2D

Discussion in 'Cinemachine' started by danthat, Oct 13, 2017.

  1. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Hello! Just grabbed RC2.1 and toying around in 2D. I have a simple Virtual Camera set up.

    When I add a transform as Follow it works fine, but I don't get any of the handy Deadzone / screen positioning associated with a LookAt

    Unfortunately, adding a LookAt transform rotates the camera ever so slightly which because I'm using a 2D Orthographic camera is causing visual issues (looks like z-fighting on all textures across the screen)

    Am I missing something? The core of Cinemachine's pull seems to be that sweet LookAt functionality, but it doesn't seem to play nice in 2D

    Any advice?
     
  2. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    I think I had a non-Camera2D Virtual Camera setup so it wasn't giving me DeadZone options for Body. Seems to be working fine by and large but I still get tearing when blending between two Virtual Cameras.
     
    Last edited: Oct 13, 2017
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Tearing? Can you show me that?
    And yes, you were using a non-2D vcam. Glad you found the right one.
     
  4. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Yeah, it should be pretty simple to recreate with a 2D setup. It's because when blending between two Virtual Cameras the Main Camera transform rotation looks to the new target ever so slightly. Is there a way of preventing that?
     

    Attached Files:

  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The angle of view of a 2D camera is defined by its transform's rotation. Make sure all your vcams have the same rotation in their transforms and you won't get any rotation shifts.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @danthat your image didn't come through properly
     
  7. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    vCams definitely all have 0, 0, 0 rotation. Main Camera is definitely showing a rotation away from 0, 0, 0 when I move between two targets.

    Capture.png *is* working. That brown section to the left should be solid brown but because the MainCam is rotated it's showing up black bits like there's something fighting for z-order. Sorry, I forgot to explain what it was you were looking at ;)
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I think you're going to have to tell me a little more about your scene for me to understand what's going on. If you could send me a little scene that shows the problem it would help a lot. I've tried to repro by creating 2 objects, with a vcam on each, and blending between them. The main camera always maintains 0 rotation, even during the blends. I've attached the scene.
     

    Attached Files:

  9. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    I think I've identified the issue. It's because I'm instantiating Virtual Cameras from a prefab (so each instasntiated character can have their own tracking camera)

    If you go to Cinemachine -> Create 2D Camera. Note the virtual camera has Orthographic view.
    Drag and drop it into a Prefabs folder, it'll convert it to a 3D virtual camera (I assume, as it now takes Field of View.)

    That's what's then allowing the camera to rotate and cause z-order fighting.

    Is it not possible to have virtual cameras as prefabs? I'll try just creating an empty game object and using AddComponent, that might work, but it's not as handy if I want to slap other stuff on the prefab.

    ??

    I appreciate your help on this ;)

    EDIT can confirm that the AddComponent process works, but it's not as convenient becuase obviously I've got to set all the Deadzone stuff in code as well. Is it possible to get working 2D prefabs?
     
    Last edited: Oct 13, 2017
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The vcam lens ortho/perspective setting simply follows whatever your main camera is doing. If it shows up as Perspective in the prefab view that's because it's not connected to an actual camera and Perspective is the default. The instance of the prefab displays ortho correctly.

    So that's not it.

    Ortho cameras (and vcams) are still allowed to rotate, in any case. If it's rotating it's because somehow you are telling it to, without realizing it. We need to figure out why. Can you describe the scene a little more? Where are the cameras, and where are the targets? Give me their co-ordinates.

    If you could zip me up the project, I think it would save some back-and-forth.
     
  11. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Thanks for your help. I created a dummy project to try to recreate the issue to zip & send, but it woks fine there. So i spent all morning hacking my code about trying to find what's causing a camera rotation on changing virtual cameras, and got nowhere.

    I first realised the issue is limited to spawned Prefab Virtual Cameras - if I add them manually to the scene and set the GameObject active/ deactivated via the Inspector they work fine - no rotated MainCam issue at all. The only difference being my in-scene cams were set to Body->Transposer and the spawned ones were Framing Transposer. So I set the prefabs, and it worked.

    In order to be helpful, I fired up the Dummy project and tried to force the error there, and got nowhere. Works fine with with either Transposed/ Framing Transposed, Main Cam stays rigidly at 0,0,0 rotation no matter what I do.

    I'm 80% sure this is a Cinemachine issue somehow. Nothing else in my main project is setting camera rotation, but I'm aware my dummy project isn't showing the issue so it's prooooobably my fault. Will keep digging and let you know if I recreate it. But for now, if anyone has a similar issue, the Transposer setting is (a) fix :)

    d
     
    Last edited: Oct 16, 2017
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you manage to get a reliable repro, please post here. I'd be interested in getting to the bottom of this.
     
  13. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Will do :)
     
  14. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Sorry, back on this because I've realised I *need* Framing Transposer and not Transposer. Here's what I've pieced together so far with the issue:

    Transposer works fine, no rotation of Main Camera.

    My setup is a little odd in that the Camera is housed in Scene1 and the characters are spawned from additively-loaded Scene2 into Scene1. Scene2 has no camera data, just TileMap stuff. I'm wondering if that's where the issue is coming from? The Camera targets AND the VirtualCams are spawned into Scene1, which has MainCamera data.

    However, Output Camera on Brain is set correctly and as far as I can tell my VirtualCams are all being set to Ortho.

    The disconnect between Transposer and Framed Transposer is interesting though?

    When one VirtualCam is set to Transposer and the other to Framed Transposer there's no issue. It's only when both are Framed Transposer. Other settings such as Orbital seem to work fine as well.

    The rotation of MainCamera blends precisely to the time taken for the position blend, leading me to believe this is definitely Cinemachine code rather than some rogue script I've forgotten about (though that's possible)

    I still can't recreate any of this in my Dummy project.

    Will keep plugging away but if there's *anything* you can think of to point me in the right direction I'd really appreciate it!
     
  15. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    RIGHT FINALLY managed to recreate the issue.

    In a scene with two SpriteRenderers, and two VirtualCams set to Framed Transposer. If I activate a camera and then move the position of the sprite, then deactivate the camera, the MainCamera will rotate to look at its new target.

    It's only when the target is moved there's an issue. If you pause the Unity Editor as the camera refocuses on Target #1, you can see it's rotating. It's worse on my main project but I think that's because it's pixel-based so quite tight. But there's definitely definitely non-0 rotation going on.

    I have this all working in a Dummy project, which I'll set uploading, but you can recreate it pretty easily.
     
    Last edited: Oct 17, 2017
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Great, I'm glad you can repro. When you upload the dummy project, I'll have a look at it.
     
  17. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Very nice. Thanks for taking the time to make this. I see why it's happening. I have a patch for you. Drop this into your project, replacing the existing file of the same name. Tell me if that solves your issue.
     

    Attached Files:

  19. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    Yaaaay looks like it works a charm. Thanks so much for this, much appreciated!
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Thanks for your effort and persistence in finding this :). It's a real issue, and I'll work on getting a more permanent fix into the next RC.
     
  21. danthat

    danthat

    Joined:
    Aug 1, 2012
    Posts:
    25
    I'm mainly relieved it wasn't something stupid I was doing ;)