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

Control TargetGroup via script

Discussion in 'Cinemachine' started by IgorDemchuk, Dec 23, 2017.

  1. IgorDemchuk

    IgorDemchuk

    Joined:
    Mar 9, 2015
    Posts:
    21
    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!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    It's just an ordinary c# array of a struct, stored as a class member:
    Code (CSharp):
    1.         CinemachineTargetGroup group = ...;
    2.         CinemachineTargetGroup.Target[] targets = group.m_Targets;
    3.         // ... manipulate targets
    4.         group.m_Targets = targets;
    You can also look into using List<CinemachineTargetGroup.Target> if you want to mess with group size. Something like this:
    Code (CSharp):
    1.         List<CinemachineTargetGroup.Target> targets = new List<CinemachineTargetGroup.Target>();
    2.         // ... manipulate targets
    3.         group.m_Targets = targets.ToArray();
     
    stereographik and IgorDemchuk like this.
  3. IgorDemchuk

    IgorDemchuk

    Joined:
    Mar 9, 2015
    Posts:
    21
    Thank you very much!
     
  4. Tekksin

    Tekksin

    Joined:
    Oct 20, 2013
    Posts:
    32
    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:

    Code (CSharp):
    1.         CinemachineTargetGroup group;
    2.         CinemachineTargetGroup.Target[] targets = group.m_Targets;
    3.         targets[0] = theTransform[0];
    4.         targets[1] = theTransform[1];
    5.         group.m_Targets = targets;
    and I got this error message:
    Code (CSharp):
    1. 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.
     
    Last edited: Apr 23, 2019
  5. Tekksin

    Tekksin

    Joined:
    Oct 20, 2013
    Posts:
    32
    nvm I got it

    Code (CSharp):
    1. public CinemachineTargetGroup ctg;
    2. public Transform[] theTransform;
    3. List<CinemachineTargetGroup.Target> targets = new List<CinemachineTargetGroup.Target>();
    4.         targets.Add(new CinemachineTargetGroup.Target { target = theTransform[0], radius = 0.8f, weight = 1f });
    5.         targets.Add(new CinemachineTargetGroup.Target { target = theTransform[1]ransform, radius = 0.8f, weight = 1f });
    6.         ctg.m_Targets = targets.ToArray ();
    7.  
     
    cassidyg and Gregoryl like this.