Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Cinemachine Target Group dynamic size

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

  1. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    78
    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:
    396
    Have you tried adjusting Soft/Dead zone?
     
  3. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    62
    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:
    78
    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:
    78
    That doesn't work.
     
  6. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    62
    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:
    78
    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:
    78

    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:
    62
    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:
    78

    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:
    62
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,733
    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.
     
  13. sebsmax

    sebsmax

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

    Redbandit

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

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,733
    Yes, there was an issue in the group composer. Lerping the weight works now.
     
  16. Dweebster

    Dweebster

    Joined:
    Aug 30, 2012
    Posts:
    63
    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:
    1,733
    Can you post a small sample that shows this happening?