Camera not Following nor Looking after change target in code

Hello. I have one virtual camera and I want this camera to follow and look to my player as soon at the player appears in the game. While the player appears I have an empty object set into the follow and LookAt variables in the Virtual Camera. I have a script that waits until the player appears and then changes the transform to target the player. I see in the inspector that the virtual Camera is actually changing the transform but as soon as I move the player the camera doesn’t follow nor looks at the player. This is the code I’m using:

public GameObject player;
public Transform PlayerStartPosition;
public CinemachineVirtualCamera virtualCamera01;

void Start(){
Invoke (“PlayerInstantiation”,3);

}

void PlayerInstantiation(){

Instantiate (player, playerStartPosition .position, playerStartPosition .rotation);
Instantiate (transportEffect , playerStartPosition .position, playerStartPosition .rotation);

virtualCamera01 .m_Follow =player .transform ;
virtualCamera01.m_LookAt = player.transform;

}

Can you show me the inspector for the Virtual Camera?

Thanks Gregory. I just found the problem. The script reference I’m using was pointing to a player prefab and not the instantiated player in the game. So I create a new transform that looks for the actual player and now the Vcam is working. See the code:

Transform playerToFollow;
public GameObject player;
public Transform playerStartPosition;

void Start () {

Invoke(“PlayerInstantiation”, 3);

}

void PlayerInstantiation(){

Instantiate (player, playerStartPosition .position, playerStartPosition .rotation);
Instantiate (transportEffect , playerStartPosition .position, playerStartPosition .rotation);
playerToFollow = GameObject.FindGameObjectWithTag (“Player”).GetComponent ();
virtualCamera01 .m_Follow =playerToFollow .transform ;
virtualCamera01.m_LookAt = playerToFollow.transform;

}

1 Like