Search Unity

Collision / Physics Integration

Discussion in 'Cinemachine' started by benthroop, May 10, 2022.

  1. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    I have spent the better part of today trying to solve collision of my game camera against the physics world and need some help.

    So my non-physics setup, which I'm happy with besides its lack of collision, relies on a Body Framing Transposer acting as an orbiting camera. By setting the X/Y position of the camera, the Transposer figures out the position and orientation. This works great as an orbiting camera and provides nice framing.

    To add collision with the world, my idea was to make a second vCam that mimics the real camera. It has a Rigidbody with a SphereCollider and uses forces to track the position of the Transposer camera. It also matches the rotation. I've set the Rigidbody to interpolate.

    As you can probably guess, this setup is resulting in jitter. However fundamentally, it works in providing collision for the camera by relying on the physics system to do the collision against the world. So it's close!

    But yeah the jitter. I feel like I've tried every permutation of the CinemachineBrain updates, where I'm setting the rotation of the camera in my own code, etc etc. I also luckily stumbled upon the Standby Update parameter to make certain that both cameras are updating every frame even though the camera doing the heavy lifting (the Transposer) is not active because it is lower priority.

    Are there obvious settings about updating that I'm missing? If not I can provide a test project. Thanks.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Why don't you use a CinemachineCollider extension on the vcam?
     
  3. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    Because I want collision of the camera itself against the world. I have a fast moving orbital camera and it often shoots through the terrain.

    Am I mistaken and that functionality does exist? I found another thread of yours where you and another couple users were passing an extension back and forth, but it didn't work for me.
     
  4. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    Oh quick update, I think I have this working. But if there is a built-in solution I would certainly prefer it if it worked better!
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    The builtin CinemachineCollider is supposed to pull the camera out of intersecting and occluding objects.
    What other thread are you referring to?
     
  6. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    Referring to this thread: https://forum.unity.com/threads/cinemachine-camera-collision.515626/

    Ok I'll give that a shot. My understanding was that the function of CinemachineCollider was to prevent objects blocking the view, not to prevent the camera from interpenetrating colliders itself. Again, I hope I'm wrong here.

    EDIT: Update

    Yeah so I tried it and it doesn't work right in this situation. The vCam in question uses only a Framing Transposer on the body and has no LookAt target. The CinemachineCollider extension does say "Avoid Obstacles requires a LookAt target". I'm not sure if that's why things don't work right. I'm able to move the camera through the terrain when rotating it quickly. Again "rotation" in this setup is just moving the Y position and the Transposer figures out how to make it orbit properly.
     
    Last edited: May 11, 2022
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Try adding a lookat target. It can be the same as the follow target.
     
  8. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    Thanks. Ok I tried that. It seems to give a brief bit of resistance as the camera hits the terrain, but then it passes through it. Here's a video: https://www.dropbox.com/s/mu5ey7095fno24t/2022-05-11 09-09-38.mkv?dl=0

    I can't begin to guess what would be the cause here. I guess the larger question is if generalized camera collision is supposed to work in Cinemachine?

    But I do have a working solution using a Rigidbody setup. A free vCam mimics the PositionCamera. This does collide reliably with the world. I can actually even use the non-physics camera until there is a collision on the physics camera, and then up the physics camera priority. This is really only necessary because I have been finding the follow behavior is not quite frame perfect. There's still a bit of lag/jitter, but it very reliably stays out of colliders.

    upload_2022-5-11_9-31-35.png

    upload_2022-5-11_9-32-56.png

    upload_2022-5-11_9-33-14.png
    upload_2022-5-11_9-34-55.png
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I like your creative solution. Thanks for posting it.

    I think the problem is that Unity physics can't de-collide the camera from the terrain if the camera is completely under the terrain. Using the RigidBody as you do keeps the camera on top.
     
    Last edited: May 16, 2022