Search Unity

Localized Transposer Damping

Discussion in 'Cinemachine' started by owen_proto, Feb 24, 2019.

  1. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    120
    I have a character that I am creating a third person camera for. The character runs around a spherical planet. I am using a Transposer body and Lock To Target binding mode with the follow transform being a child of the character in the position I want the camera to be. I am using the character's transform as the world up override.

    I would also like to have some Z Damping going on. The problem I'm running into is that the planet also rotates on its axis and the camera position behaves as if the character is moving (because it is - along with the rotating planet) even when it is standing still (relative to the planet's surface).

    Is there any relatively straightforward way to feed the cinemachine brain script an input velocity vector to calculate damping with? That way I would be able to use player input to drive the damping perhaps. I could also do this to a "camera mount" transform and have the virtual camera hard lock, but it would be nice to keep all the camera functionality together if possible.

    Also - is there any way to set the maximum distance the camera can get from the follow transform? It would be nice to, say, have a slower damping speed but not let the camera get too far away from the character.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Damping is controlled directly on the vcam, the brain knows nothing about it. Working with the vcam, you have a few options.
    • You could add a script to your vcam that scales the vcam damping based on player input.
    • Another option might be to use the vcam's OnTargetObjectWarped() method to continuously warp the target as the platen moves. The vcam will be warped along with it, bypassing the damping. However, this would apply only to position damping, not to orientation damping. Is your planet very large relative to the vcam/player distance? If so, then you might get away with ignoring the rotational damping.
    • If you want to go all out, you could copy and modify the CinemachineTransposer component (drop your new component in the project and the vcam inspector will pick it up) implementing your own follow behaviour with specialized damping
    For the maximum distance, you could implement a simple CinemachineExtension to clamp the vcam distance. Here is an example of one that ensures a minimum distance. Easy to change it to one that implements a max distance:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that clamps target disatance
    6. /// </summary>
    7. [ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class ClampVcamDistance : CinemachineExtension
    9. {
    10.     [Tooltip("Clamp the vcam's distance from the target to at least this amount")]
    11.     public float m_MinDistance = 10;
    12.  
    13.     private void OnValidate()
    14.     {
    15.         m_MinDistance = Mathf.Max(0, m_MinDistance);
    16.     }
    17.  
    18.     protected override void PostPipelineStageCallback(
    19.         CinemachineVirtualCameraBase vcam,
    20.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    21.     {
    22.         if (stage == CinemachineCore.Stage.Body && VirtualCamera.Follow != null)
    23.         {
    24.             var dir = state.RawPosition - VirtualCamera.Follow.position;
    25.             float d = dir.magnitude;
    26.             if (d < m_MinDistance)
    27.                 state.RawPosition = VirtualCamera.Follow.position + dir.normalized * m_MinDistance;
    28.         }
    29.     }
    30. }
    31.  
     
  3. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    120
    I really appreciate your guidance! Thank you so much!