Search Unity

Group Target to nearest

Discussion in 'Cinemachine' started by Kelvynlee, Jul 20, 2020.

  1. Kelvynlee

    Kelvynlee

    Joined:
    Oct 11, 2019
    Posts:
    5
    I'm trying to do the camera stay between two objects wich is nearest.
    I added the group target cinemachine, but i want to do to nearest object, on player range



    I tryed to AddMember in script


    But when i started, he add the objects a lot times because of "InvokeRepeating"

    And when i left the range, he dont remove those objects on group

    I need help, there a way more easier to do this ? or fix this ?
    -I'm sorry about my bad english
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Don't add it if it is already a member.

    You can also consider putting all the nearby enemies into the group, and adjusting their weights as a function of distance, so that nearer targets will be given more significance (targets with 0 weight are ignored). That way, if 2 enemies are equally near, they will both get focus, and you will avoid jitter in the case where the nearest enemy alternates from one to the other.
     
    Kelvynlee likes this.
  3. Kelvynlee

    Kelvynlee

    Joined:
    Oct 11, 2019
    Posts:
    5

    How do i check if its is already a member ?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    group.FindMember()
     
    Kelvynlee likes this.
  5. Kelvynlee

    Kelvynlee

    Joined:
    Oct 11, 2019
    Posts:
    5
    The last question, sorry.
    How do i RemoveMember after the first parameter, like this, remove all Transforms after the Player?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    group.m_Targets is just an array. Maybe the simplest and most efficient thing for you to do is to clear it and re-add all required targets every frame.

    Something like:
    Code (CSharp):
    1. List<CinemachineTargetGroup.Target> targets;
    2.  
    3. // pseudo-code
    4. targets.Clear();
    5. targets.Add(player)
    6. for each enemy { targets.Add(enemy) }
    7.  
    8. group.m_Targets = targets.ToArray()
    9.