Search Unity

  1. Unity 2018.3 is now released.
    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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Cinemachine Target Group , Add Target in runtime when a player joins game

Discussion in 'Cinemachine' started by karlanton98, Sep 22, 2018.

  1. karlanton98

    karlanton98

    Joined:
    Nov 6, 2016
    Posts:
    3
    Exactly as the title says, how do i (in code) add a target to the cinemachine target group script in runtime?
     
  2. karlanton98

    karlanton98

    Joined:
    Nov 6, 2016
    Posts:
    3
    Code (CSharp):
    1. targetGroup = GameObject.Find("TargetGroup1").GetComponent<CinemachineTargetGroup>();
    2.  
    3. Cinemachine.CinemachineTargetGroup.Target target;
    4. target.target = "Target to add";
    5. target.weight = "the weight";
    6. target.radius = "the radius";
    7.  
    8.         for (int i = 0; i < targetGroup.m_Targets.Length; i++)
    9.         {
    10.             if (targetGroup.m_Targets[i].target == null)
    11.             {
    12.                 targetGroup.m_Targets.SetValue(target, i);
    13.                 return;
    14.             }
    15.         }
    I fixed it by doing this, and add multiple NULL targets in the targetgroup in the editor.
     
    mitaywalle likes this.
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,845
    Yes, well done. You could also do something like
    Code (CSharp):
    1. targetGroup.m_Targets = new CinemachineTargetGroup.Target[blabla];
    instead of putting the empty ones in the inspector.
     
    Last edited: Dec 10, 2018
    mitaywalle likes this.
  4. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    60

    upload_2018-12-7_14-9-21.png - my custom List.
    upload_2018-12-7_14-9-54.png - target group Array.
    for some reason target transform is not set. Some mistakes with value-type creation process, I guess, but it's same with TS way!

    Here is Code:
    Code (CSharp):
    1.         var index = -1;
    2.  
    3.         var count = TargetGroup.m_Targets.Length;
    4.    
    5.         for (int i = 0; i < count; i++)
    6.         {
    7.             if (TargetGroup.m_Targets[i].target == null)
    8.             {
    9.                 index = i;
    10.                 break;
    11.             }
    12.         }
    13.    
    14.         var target = new CinemachineTargetGroup.Target {target = newTarget, radius = radius, weight = weight};
    15.  
    16.         if (index == -1)
    17.         {
    18.             Targets.Add(target);
    19.             index = Targets.Count-1;
    20.             TargetGroup.m_Targets = Targets.ToArray();
    21.         }
    22.         else
    23.         {
    24.             Targets[index] = target;
    25.             TargetGroup.m_Targets.SetValue(target, index);
    26.         }
    27.  

    More over, if I print log at the end:
    Code (CSharp):
    1. Debug.LogFormat("added new target: {0}",TargetGroup.m_Targets[index].target.name);
    It will tell, that target exist, and print its name
     
    Last edited: Dec 7, 2018
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,845
    @mitaywalle I'm not sure whether you're asking a question. Also, why does your target group inspector look strange? It's as if you're not getting the CM custom inspector for it.