Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Camera jumping when adding new object to target group

Discussion in 'Cinemachine' started by SpottedAlien456, Apr 6, 2023.

  1. SpottedAlien456

    SpottedAlien456

    Joined:
    Sep 21, 2022
    Posts:
    5
    I am trying to change targets by adding them to a target group and Lerping in/out their weights and radius to create a smooth transition. However, the camera jumps as soon as the new target is added despite the weight starting at 0. Any settings I am missing that would create a smooth transition?

    Code (CSharp):
    1.  
    2. private IEnumerator AddToTargetGroup(Transform futureTarget, float weight, float radius)
    3.     {
    4.         cineTargetGroup.AddMember(futureTarget, 0f, startTargetRadius);
    5.         int index = cineTargetGroup.FindMember(futureTarget);
    6.         //CinemachineTargetGroup.Target t = cineTargetGroup.m_Targets[index];
    7.         Debug.Log("Index is " + index);
    8.         float timeElapsed = 0f;
    9.         while (timeElapsed < lerpDuration)
    10.         {
    11.             Debug.Log("in coroutine");
    12.             timeElapsed += Time.deltaTime;
    13.             //t.weight = Mathf.Lerp(0f, weight, timeElapsed / lerpDuration);
    14.             cineTargetGroup.m_Targets[index].weight = Mathf.Lerp(0f, weight, timeElapsed / lerpDuration);
    15.             cineTargetGroup.m_Targets[index].radius = Mathf.Lerp(startTargetRadius, radius, timeElapsed / lerpDuration);
    16.             Debug.Log("Weight is " + cineTargetGroup.m_Targets[index].weight);
    17.  
    18.             yield return null;
    19.         }
    20.         cineTargetGroup.m_Targets[index].weight = weight;
    21.         cineTargetGroup.m_Targets[index].radius = radius;
    22.     }
    23.     private IEnumerator RemoveFromTargetGroup(Transform currentTarget)
    24.     {
    25.         int index = cineTargetGroup.FindMember(currentTarget);
    26.         //CinemachineTargetGroup.Target t = cineTargetGroup.m_Targets[index];
    27.         float weight = cineTargetGroup.m_Targets[index].weight;
    28.         float radius = cineTargetGroup.m_Targets[index].radius;
    29.  
    30.         float timeElapsed = 0f;
    31.         while (timeElapsed < lerpDuration)
    32.         {
    33.             cineTargetGroup.m_Targets[index].weight = Mathf.Lerp(weight, 0f, timeElapsed / lerpDuration);
    34.             cineTargetGroup.m_Targets[index].radius = Mathf.Lerp(radius, startTargetRadius, timeElapsed / lerpDuration);
    35.             timeElapsed += Time.deltaTime;
    36.             yield return null;
    37.         }
    38.         cineTargetGroup.m_Targets[index].weight = 0f;
    39.         cineTargetGroup.m_Targets[index].radius = startTargetRadius;
    40.         cineTargetGroup.RemoveMember(currentTarget);
    41.     }
    42.  
     
  2. SpottedAlien456

    SpottedAlien456

    Joined:
    Sep 21, 2022
    Posts:
    5
    Naturally, as soon as I posted this I realized it was coming from my player rotation instantly changing to face the new target. Go cinemachine
     
    Gregoryl likes this.