Control TargetGroup via script

Hello!

First of all, thank you very much for making Cinemachine!

How can I populate existing TargetGroup and set Weight and Radius of objects in TargetGroup via script?
I’m using Cinemachine 2.1.10 and Unity 2017.3.
I couldn’t find any examples. Cinemachine API document is too confusing for me, it never actually helped me.

Thank you!

It’s just an ordinary c# array of a struct, stored as a class member:

        CinemachineTargetGroup group = ...;
        CinemachineTargetGroup.Target[] targets = group.m_Targets;
        // ... manipulate targets
        group.m_Targets = targets;

You can also look into using List<CinemachineTargetGroup.Target> if you want to mess with group size. Something like this:

        List<CinemachineTargetGroup.Target> targets = new List<CinemachineTargetGroup.Target>();
        // ... manipulate targets
        group.m_Targets = targets.ToArray();
2 Likes

Thank you very much!

What do you write in “//…manipulate targets”
?

I’m trying to set them to transforms, but your example doesn’t really help. I’m not sure if a target can be read as a transform.

I tried:

        CinemachineTargetGroup group;
        CinemachineTargetGroup.Target[] targets = group.m_Targets;
        targets[0] = theTransform[0];
        targets[1] = theTransform[1];
        group.m_Targets = targets;

and I got this error message:

Cannot implicitly convert type `UnityEngine.Transform' to `Cinemachine.CinemachineTargetGroup.Target'

Filling these things out should be easier to find, but I’ve got a hundred tabs open and I’m still clueless.

nvm I got it

public CinemachineTargetGroup ctg;
public Transform[] theTransform;
List<CinemachineTargetGroup.Target> targets = new List<CinemachineTargetGroup.Target>();
        targets.Add(new CinemachineTargetGroup.Target { target = theTransform[0], radius = 0.8f, weight = 1f });
        targets.Add(new CinemachineTargetGroup.Target { target = theTransform[1]ransform, radius = 0.8f, weight = 1f });
        ctg.m_Targets = targets.ToArray ();
2 Likes