Search Unity

Adding PreFabs to the Cinemachine Target Group

Discussion in 'Cinemachine' started by Marythu, Apr 14, 2021.

  1. Marythu

    Marythu

    Joined:
    Mar 29, 2021
    Posts:
    13
    Hello everybody,

    i wanted to know if there is any way to add PreFabs of my player to the Target Group in Cinemachine. In my project i spawn the players from a prefab, because i want a flexible amount of them. But there starts the problem. As i want the camera to be dynamic and not static it would be very cool if every player is always in the frame. But because i dont have a certain amount of players i create them from a prefab. It seems that unity has no way to add these players from theprefab to the target group automatically (or manually per code) or am i missing something?

    Thank you so much in advance!
    M
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    DefrogEver likes this.
  3. Marythu

    Marythu

    Joined:
    Mar 29, 2021
    Posts:
    13
    Thank you for the quick answer! Where is best place to put the code? Onto the Player or the TargetGroup? And how can i get the right reference to the instance?
     
    Last edited: Apr 14, 2021
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    CinemachineTargetGroup, yes.

    I presume that you have some code that creates instances of the prefab. At that moment, you have the instance and can add it to the group immediately, or store it somewhere to be added in the future.
     
  5. Marythu

    Marythu

    Joined:
    Mar 29, 2021
    Posts:
    13
    group = GameObject.Find("CinemachineTargetGroup").GetComponent<CinemachineTargetGroup>();
    unity.png
    If i use the usual way to reference CinemachineTargetGroup Script to use group.AddMember Unity says "CinemachineTargetGroup" (Component) could not be found. If i create a second script on the CinemachineTargetGroup Object as a "bridge" i can find the bridge, but not the CinemachineTargetGroup Component.

    Am i missing something?
     
    Last edited: Apr 14, 2021
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    It's just a component like any other, nothing special there. I don't know what you're doing wrong.

    GameObject.Find("CinemachineTargetGroup")
    is very inefficient, you might want to think of a better way to keep a pointer to your group. It's also error-prone. Could it be that you have multiple objects called "CinemachineTargetGroup"?
     
  7. Marythu

    Marythu

    Joined:
    Mar 29, 2021
    Posts:
    13
    Hmm thats strange. I even deleted it and created a new one, but the component is still untraceable.
     
  8. kyanyap

    kyanyap

    Joined:
    Oct 4, 2021
    Posts:
    3
    that means i need to write the group.AddMember after this?
    upload_2022-6-3_17-21-29.png
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    yes