Cinemachine Target Group dynamic size

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: - Find & Share on GIPHY

Have you tried adjusting Soft/Dead zone?

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)

I tried this in the CinemachineTargetGroup.cs and the camera goes crazy.

        public virtual void Update()
        {
            switch (m_PositionMode)
            {
                case PositionMode.GroupCenter:
                    transform.position = Vector3.Lerp(transform.position, BoundingBox.center, Time.deltaTime * damping);
                    break;
                case PositionMode.GroupAverage:
                    transform.position = Vector3.Lerp(transform.position, CalculateAveragePosition(), Time.deltaTime * damping);
                    break;
            }
            switch (m_RotationMode)
            {
                case RotationMode.Manual:
                    break;
                case RotationMode.GroupAverage:
                    transform.rotation = Quaternion.Lerp(transform.rotation, CalculateAverageOrientation(), Time.deltaTime * damping);
                    break;
            }
        }

That doesn’t work.

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:
3166134--241077--upload_2017-7-31_15-16-7.png

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?

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.

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)

Vector3 totalvector;
float count = 0;

foreach (var enemy in enemieslist ) {
totalvector += enemy.transform.position * enemy.weight;
count += 1 * enemy.weight;
}

Vector3 center = totalvector /count;

float radius = 0;

foreach (var enemy in enemieslist ) {
radius = Mathf.Max( vector3.distance( enemy.transform.position , center ), radius );
}

then you lerp on the two values center and radius

1 Like

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.

@xDavidLeon , Did it work for you in the end?

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.

1 Like

Yes, It was the first thing that came to my mind, camera was still jittering :frowning:

https://forum.unity3d.com/threads/individual-blend.485247/#post-3161502

Hey, i know it is a little late, but did you find a solution to the problem? :S

Yes, there was an issue in the group composer. Lerping the weight works now.

1 Like

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?

Can you post a small sample that shows this happening?

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://discussions.unity.com/t/730538