Hi, I have a script called camera follow. It was working but when I added multiple characters and use them according to some value using if statements it doesn’t work. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public GameObject player;
public GameObject Jet1;
public GameObject Grim;
public GameObject Srm;
public GameObject A1R;
public GameObject TypeP2077;
public Vector3 offset;
// Use this for initialization
void Start()
{
int playerSelection = PlayerPrefs.GetInt("CharacterIndex");
if(playerSelection == 0)
{
Jet1 = player;
}
else if(playerSelection == 1)
{
player = Grim;
}
else if(playerSelection == 2)
{
player = Srm;
}
else if(playerSelection == 3)
{
player = A1R;
}
else if(playerSelection == 4)
{
player = TypeP2077;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (player != null)
{
Vector3 camPos = transform.position;
Vector3 desiredPos = player.transform.position;
Vector3 smoothedPos = Vector3.Lerp(camPos, desiredPos, 0.125f);
transform.position = new Vector3(smoothedPos.x, transform.position.y, transform.position.z);
}
}
}
So what is the problem and how can I fix it. the offset is x at 0 y at 0 and z at -10. I have referenced everything correctly.