Search Unity

Framing Transposer, only zooming the target but not following it

Discussion in 'Cinemachine' started by ColdHand, Apr 16, 2021.

  1. ColdHand

    ColdHand

    Joined:
    Apr 10, 2018
    Posts:
    7
    I have this scene of a 2D platform fighter type of game where you can play it with multiplayer, a small stage so they are only able to move and jump around that stage. I have the target group attached to the Follow configuration and also set up the framing transposer and camera confiner.

    The game works fine, but I decided to change how the camera works because if a player jumps too high while another player is still on the platform, the camera will display the stage at the bottom, and I find it uncomfortable because I will put the UI of each player's status there, at the bottom and the UI will interfere with the visual appearance of the stage.

    So what I want to achieve is to have the camera steady in the center, and only perform zoom in/out depends on the average player positions. I read the documentation, I found there is a setting called adjustment mode, where it seems I can choose if the camera would not follow the target and only perform the zoom. But I don't find that setting in the framing transposer, even though I have set the target group as the object to follow.

    Has the setting been deleted? If so, is there any workaround to achieve this? Thank you!
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You can achieve this by:
    1. Adding an invisible target to the TargetGroup. This invisible target is going to represent the center. This is the only transform in the target group.
    2. You need to dynamically adjust the radius of this target to be equal to the distance of the farthest target from the center.
    A simple script that achieves step 2. You need to attach this script to the TargetGroup's GameObject and set the player transforms.
    Code (CSharp):
    1. using Cinemachine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AdjustRadius : MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         m_TargetGroup.m_Targets[0].radius =
    11.             DistanceToFarthestPlayer(m_TargetGroup.m_Targets[0].target.position);
    12.     }
    13.  
    14.     public List<Transform> players;
    15.     float DistanceToFarthestPlayer(Vector3 center)
    16.     {
    17.         float maxDistance = 0f;
    18.         foreach (var player in players)
    19.         {
    20.             float sqrDistance = (center - player.position).sqrMagnitude;
    21.             if (sqrDistance > maxDistance)
    22.             {
    23.                 maxDistance = sqrDistance;
    24.             }
    25.         }
    26.         return Mathf.Sqrt(maxDistance);
    27.     }
    28.  
    29.     CinemachineTargetGroup m_TargetGroup;
    30.     void Awake()
    31.     {
    32.         m_TargetGroup = GetComponent<CinemachineTargetGroup>();
    33.     }
    34. }
    It will look like this:
    upload_2021-4-16_17-27-53.png

    Moreover, if you'd like to control the min and max orthographic size, then you need to set that on FramingTransposer. You will see this option iff you have a TargetGroup as the Follow target.
    upload_2021-4-16_17-38-38.png
     
    Last edited: Apr 19, 2021
    Gregoryl and ColdHand like this.
  3. ColdHand

    ColdHand

    Joined:
    Apr 10, 2018
    Posts:
    7
    Never thought about this before so thank you so much! I'll try to implement the logic, and come back if there is anything I want to add! Cheers!