Search Unity

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:
    7
    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:
    7
    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.
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    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
  4. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253

    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:
    7,730
    @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.
     
  6. Poyoma

    Poyoma

    Joined:
    Apr 8, 2014
    Posts:
    1
    Hey Gregoryl and Karlanton, is there a description of what Everything Karlantons solution means? I’m pretty new to Unity, switching from Unreal blueprints. I’m hoping to add and remove targets at runtime from a target group.
     
    dwilking and IlyaDidjital like this.
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    zainalchia, oriolmanya and Jos-Yule like this.
  8. oriolmanya

    oriolmanya

    Joined:
    Jul 4, 2012
    Posts:
    50
    Good addition @Gregoryl. For sake of completness, FindMember() is also very useful to make sure the Transform you want to include isn't already there. Or check if it's there if you want to remove it.

    Edit: Remember that Returns -1 if not a member, I was checking against null...
     
    Last edited: Apr 18, 2020
    Gregoryl likes this.
  9. Danielsantalla

    Danielsantalla

    Joined:
    Jan 7, 2015
    Posts:
    75
    How do you use those methods?
    I'm doing this and it doesn't add them to the target group
    Code (CSharp):
    1.    
    2. public CinemachineTargetGroup targetbrain;
    3.  
    4.     void Start()
    5.     {
    6.         StartCoroutine("LateStart");
    7.     }
    8.  
    9.     IEnumerator LateStart()
    10.     {
    11.         yield return new WaitForSeconds(11);
    12.  
    13.        //Find players in scene
    14.         p1 = GameObject.Find("Player 1").transform;
    15.         p2 = GameObject.Find("Player 2").transform;
    16.  
    17.         yield return new WaitForSeconds(1);
    18.         //Add members to follow group
    19.    
    20.         targetbrain.AddMember(p1, 1f, 0f);
    21.         targetbrain.AddMember(p2, 1f, 0f);
    22.  
    23.     }
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    @Danielsantalla that code looks good to me. It should work correctly, provided that nothing is null (maybe you should add some checks?)