Player is in dontdestroyonload, vcam is in scene. Vcam can't follow/look at player

Hi!

I’ve been having an issue with Virtual Cameras and I hope you can help me figure this out, I’ve tried to find a solution and I’ve search around on the internet and I haven’t find anything useful. So let me explain! :slight_smile:

The player prefab has a virtual camera (player camera) that follow the player, and that player prefab gets instantiated to the game with dontdestroyonload. That works all fine and dandy.

Then I have another virtual camera (corridor camera) in the scene that I’ve set to follow and look at the Player prefab.
I have to add the player prefab through the Project explorer, because it doesn’t appear when I click the “prefab finder” next to the Look at/follow input field.

Then when I run the game and I enter a trigger that changes the priority to corridor camera, that virtual camera doesn’t follow the player.

My guess is that because the player is outside the scene the virtual camera doesn’t find the player and it doesn’t properly follow it.

So… what do I do in this instance?

You can’t create a link in your scene to a prefab - you can only link to prefab instances in the same scene.

A good way to work around this is to create a CinemachineTargetGroup in your scene, which you use as target for the vams that are in the scene. When you spawn the player, also add that spawned player instance to the target group. That way, all the vcams in the scene will automatically follow the instanced player.

1 Like

Hello! Sorry for the delay. I have finally been able to test this but I didn’t had any success.

I created a “target group camera”, the virtual camera has a “look at” and “follow” set to the Target Group. Then I add the target player to that target group by dragging and dropping the player prefab from the Project explorer to the target group. The behavior is the same as before, the cameras don’t follow the player and they are stuck looking at 0,0.

Reminder that the player is not on the scene, it gets added on runtime and is instantiated in the dontdestroyonload.

Dragging in the prefab won’t work, for the same reasons as before.

You have to do it in code: when your code instances the player, add that instance programmatically to the target group with CinemachineTragetGroup.AddMember().

1 Like

Yes, that’s what I did, here’s the code:

using Cinemachine;
using UnityEngine;

public class AddTargetToGroup : MonoBehaviour {
 
    private CinemachineTargetGroup targetGroup;
    public Transform player;

    void Awake() {
        targetGroup = GameObject.Find("TargetGroupPlayer").GetComponent<CinemachineTargetGroup>();

        //Add members to follow group
        targetGroup.AddMember(player, 1f, 0f);
    }
}

I see how the player gets added to the TargetGroup, but the virtual camera just doesn’t follow the player.

Before runtime:

At runtime:

The virtual camera at runtime. Note the “Ross” player is set to follow and look at.

When I reach the area where I want to enable this virtual camera, (let’s say that it’s in 100, 6, 100), a trigger changes the virtual camera priority to a number greater than the other virtual cameras and I expect that the virtual camera will follow the player, but the camera travels to 0, 7, -0.5 and it’s stuck there.

I also tried this approach with the exact same result.

It looks like as if the “Look at” and “follow” doesn’t update the position of the player as it moves and they just know where the player first spawned.

So I finally got it working using the method described in this thread , which previously didn’t work for me but I managed to make it work…

So that’s it! Issue solved. :smile:

Thank you Gregoryl for your time!

1 Like