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

Cinemachine Target Group dynamic size

Discussion in 'Cinemachine' started by xDavidLeon, Jul 31, 2017.

  1. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123
    Hi there,

    I'm using the Cinemachine Target Group component to create a combat camera that targets all enemies in range (as well as the player).

    As enemies move, they'll enter/exit the combat range, so I modify the Target list in Cinemachine Target Group in real time. Main issue is, when adding or removing targets, there's a strong cut when the camera readjusts its lookat target.

    Video: https://giphy.com/gifs/3o6vXT4sQVBNkZBz2g/fullscreen
     
  2. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    Have you tried adjusting Soft/Dead zone?
     
  3. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    I had the same issue, to fix it I ended up by calculating the center points and the radius of all the enemies.
    Then I used a lerp on a gameobject to calculate the "smoothed" position. this same object is attached to the target group. And then via code I change it's radius with a lerp on it. (Dunno if it's clear)
     
  4. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123
    I tried this in the CinemachineTargetGroup.cs and the camera goes crazy.

    Code (CSharp):
    1.         public virtual void Update()
    2.         {
    3.             switch (m_PositionMode)
    4.             {
    5.                 case PositionMode.GroupCenter:
    6.                     transform.position = Vector3.Lerp(transform.position, BoundingBox.center, Time.deltaTime * damping);
    7.                     break;
    8.                 case PositionMode.GroupAverage:
    9.                     transform.position = Vector3.Lerp(transform.position, CalculateAveragePosition(), Time.deltaTime * damping);
    10.                     break;
    11.             }
    12.             switch (m_RotationMode)
    13.             {
    14.                 case RotationMode.Manual:
    15.                     break;
    16.                 case RotationMode.GroupAverage:
    17.                     transform.rotation = Quaternion.Lerp(transform.rotation, CalculateAverageOrientation(), Time.deltaTime * damping);
    18.                     break;
    19.             }
    20.         }
     
  5. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123
    That doesn't work.
     
  6. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    Indeed, I was suggesting to have an external Game object that does that! If you touch the CinemachineTargetGroup, it's freaking out!
    This is what I have:
    upload_2017-7-31_15-16-7.png
     
  7. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123
    It's probably the lack of sleep, but I still can't get it to work properly. I've got an "empty" gameobject with just the Cinemachine Taget Group. The script only has the modifications I've shown earlier to the Update function. The combat camera (Cinemachine Free Look) has the Player as Follow and the new 'TargetGroup gameobject' as LookAt.

    Still getting the crazy jitter. Could you perhaps share your modifications to CinemachineTargetGroup.cs?
     
  8. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123

    Ok, I think I get what you did. Instead of having an additional target per enemy, you are merging all enemy positions in a virtual target (gameobject). The issue with that is that if I want to add more weight to one of the enemies, I can't do it, so the camera ends up looking to the empty space in the middle of all enemies. I could add a custom weight to that virtual position 'tho.. I'll keep investigating.
     
  9. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    no as you are making your own "Merging", you can define your the weight of each enemy,
    this is untested pseudo-code so you get the idea (I'm not in front of my computer)

    Code (CSharp):
    1. Vector3 totalvector;
    2. float count = 0;
    3.  
    4. foreach (var enemy in enemieslist ) {
    5. totalvector += enemy.transform.position * enemy.weight;
    6. count += 1 * enemy.weight;
    7. }
    8.  
    9. Vector3 center = totalvector /count;
    10.  
    11. float radius = 0;
    12.  
    13. foreach (var enemy in enemieslist ) {
    14. radius = Mathf.Max( vector3.distance( enemy.transform.position , center ), radius );
    15. }
    16.  
    then you lerp on the two values center and radius
     
    xDavidLeon likes this.
  10. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123

    Yeah, that's exactly what I did. It could be interesting if the Cinemachine dev could add native smoothing and support for dynamic targets, I'm sure other devs would appreciate it.
     
  11. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Did you try just lerping the target group member's weight from/to 0 to bring it in/out of the group? Only add/remove members if their weights are 0.
     
    stefan-velnita likes this.
  13. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    Last edited: Jul 31, 2017
  14. Redbandit

    Redbandit

    Joined:
    Mar 1, 2017
    Posts:
    1
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Yes, there was an issue in the group composer. Lerping the weight works now.
     
    nicolascparreiras likes this.
  16. PrettyFlyGames

    PrettyFlyGames

    Joined:
    Aug 30, 2012
    Posts:
    74
    I have the same issue and I lerp the weights from zero but as soon as the weight is a little bit more than zero the camera jerks a lot and then eases the targets in.

    I have hardlocked the camera to a point and I'm using Group composer to for the aim part.

    Any suggestions how this can be solved?
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Can you post a small sample that shows this happening?
     
  18. PvtPrivate

    PvtPrivate

    Joined:
    Jun 7, 2015
    Posts:
    2
    I also have this issue and it seems to me to be a bug in the target group. I have created a thread explaining it here https://forum.unity.com/threads/tar...ing-a-targets-weight-to-and-from-zero.621598/