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

Clamp camera Aim rotation for ClearShot cameras

Discussion in 'Cinemachine' started by Deleted User, Jan 17, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello

    we are trying to use Clear Shot in a third person game, with a series of fixed cameras (no Body, Aim Composer) following the player.

    What we would like to do is to limit the virtual camera rotation between limits to have a "good" shot and not follow the player everywhere; something like a Confiner, but for the Aim and not the Body.

    Is there a proper way to do that inside Cinemachine? or do I have to write an Extension?

    Second, I would also need a max camera-target distance: if target is more distant, the shot becomes inactive.

    Thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Unfortunately Cinemachine does not ship an Aim confiner out of the box. You would have to implement one as a custom CinemachineExtension. You could take a look at the CinemachineConfiner extension code and use it as a starting point. Feel free to post your code here and we could have a look at it to make sure you're on the right track.

    For the second question, inside the CinemachineCollider there is an "optimal target distance" setting. If this is nonzero, it represents the preferred camera-to-target distance. Vcams get higher scores when the target is closer to this distance and so will be preferred by ClearShot. It's not exactly "max distance" but it might do the job for you.
     
    Last edited: Jan 19, 2019
  3. Deleted User

    Deleted User

    Guest

    Ok I got something working:

    Code (CSharp):
    1. protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    2.     {
    3.         // Apply after the camera has been aimed.
    4.         // To apply offset before the camera aims, change this to Body
    5.         if (stage == CinemachineCore.Stage.Aim)
    6.         {
    7.             if (apply)
    8.             {
    9.                 Quaternion rot = state.RawOrientation;
    10.                 Vector3 euler = rot.eulerAngles;
    11.                 euler.y = Mathf.Clamp(euler.y, topLeftLimit.y, bottomRightLimit.y);
    12.                 rot.eulerAngles = euler;
    13.                 state.RawOrientation = rot;
    14.             }
    15.         }
    16.     }
    Camera is clamped on the y rotation... is there a way to force the ClearShot to remove a camera as LiveChild so I can "deactivate" it if the player is outside of bounds?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Code looks good.

    You can use the CinemachineCollider that's connected to the clearshot. If the LookAt target is outside the frustum of the vcam, then the shot will be downdgraded and another vcam will be chosen, if one is available. Is that not happening?
     
  5. Deleted User

    Deleted User

    Guest

    Yes, that works, but I would like to have more control... If I want to implement the max camera distance, if the player is in frustum but 100 meter far from camera, I want to tell the ClearShot to ignore him.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can add some logic to your custom extension. Have it check the target distance and downgrade state.ShotQuality if target is far away.
     
  7. Deleted User

    Deleted User

    Guest

    Right, but downgrading ShotQuality is not enough for our needs.
    We are trying to use a system a bit complex, that mixes a Freelook inside a State Driven Camera and different sets of Clear Shots, so we need a way to check that a ClearShot has no active cameras (based on our special criterias like maxDistance), so we can raise the priority of our State Driven cameras.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You don't have to limit your logic to max camera distance, you can check any criteria you like. The point being: the way to control what vcam the ClearShot chooses is to manipulate the Shot Quality of its Children. Highest quality wins.
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The ClearShot, once it has chosen its best-quality active vcam, will report that shot's quality in its own state. So you can check the shotQuality of the ClearShot itself, and if it's below some threshold, you can conclude that it doesn't have a good shot.

    Or better still, if it's a ClearShot inside a ClearShot that also has SDCs, then the SDCs will be preferred in the outer ClearShot because their qualities will be better that the innner ClearShot that has no good shots. If you set it up right, the choice could happen automatically.
     
    LMatyas likes this.
  10. Deleted User

    Deleted User

    Guest

    Ok thanks, this is the solution I was thinking to use.

    Right, I'll see if our game fits this structure.

    Thanks for your replies!