Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How could I have the camera be attached to different characters in different positions?

Discussion in 'Prefabs' started by RaffaeleOz1, Mar 4, 2023.

  1. RaffaeleOz1

    RaffaeleOz1

    Joined:
    Sep 6, 2022
    Posts:
    1
    Hello, I'm a Unity and C# beginner and I'm working on a game for a school project. I've mainly followed tutorials so far

    public class LoadCharacter : MonoBehaviour
    {
    [SerializeField]
    CameraFollow cameraFollow;

    public GameObject[] characterPrefabs;
    public Transform spawnPoint;
    public TMP_Text label;

    void Start()
    {
    int selectedCharacter = PlayerPrefs.GetInt("selectedCharacter");
    GameObject prefab = characterPrefabs[selectedCharacter];
    GameObject clone = Instantiate(prefab, spawnPoint.position, Quaternion.identity);
    label.text = prefab.name;

    // In the same way, we can easily update the camera on scene load
    Character character = clone.GetComponent<Character>();
    cameraFollow.SetFollowTarget(
    character.transform,
    character.OffSet);
    }
    }