Nortonb
New member
- Joined
 - Nov 24, 2022
 
- Messages
 - 3
 
- Programming Experience
 - 1-3
 
Hi, I'm creating a 2d runner game where the camera follows the player, i have 3 prefab characters which you choose at the main menu, but when the game starts the camera loses the reference to the player and won't follow. I know this is normal, but I can't figure out how to implement this. any help would be greatly appreciated.
//THIS SCRIPT IS ATTACHED TO MAIN MENU SCENE - CANVAS - CHARACTER SELECTION GRID THAT I CREATED.
----------------------------------------------------------------------------------------------------------
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
//THIS SCRIPT IS ATTACHED TO A EMPTY GAME OBJECT IN PLAY MODE.
-----------------------------------------------------------------------------------------------------------------
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
//THIS SCRIPT IS ATTACHED TO THE MAIN CAMERA IN PLAY MODE.
----------------------------------------------------------------------------------------------------------------
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			//THIS SCRIPT IS ATTACHED TO MAIN MENU SCENE - CANVAS - CHARACTER SELECTION GRID THAT I CREATED.
----------------------------------------------------------------------------------------------------------
			
				C#:
			
		
		
		public class CharacterSelectionUI : MonoBehaviour
{
    public GameObject optionPrefab;
    public Transform prevCharacter;
    public Transform selectedCharacter;
    private void Start()
    {
        foreach (Character c in MenuManager.instance.characters)
        {
            GameObject option = Instantiate(optionPrefab, transform);
            Button button = option.GetComponent<Button>();
            button.onClick.AddListener(() =>
            {
                MenuManager.instance.SetCharacter(c);
                if (selectedCharacter != null)
                {
                    prevCharacter = selectedCharacter;
                }
                selectedCharacter = option.transform;
            });
            Text text = option.GetComponentInChildren<Text>();
            text.text = c.name;
            Image image = option.GetComponentInChildren<Image>();
            image.sprite = c.icon;
        }
    }
    private void Update()
    {
        if(selectedCharacter != null)
        {
            selectedCharacter.localScale = Vector3.Lerp(selectedCharacter.localScale, new
                Vector3(1.2f, 1.2f, 1.2f), Time.deltaTime * 10);
        }
        if (prevCharacter != null)
        {
            prevCharacter.localScale = Vector3.Lerp(prevCharacter.localScale, new
                Vector3(1f, 1f, 1f), Time.deltaTime * 10);
        }
    }
}
	//THIS SCRIPT IS ATTACHED TO A EMPTY GAME OBJECT IN PLAY MODE.
-----------------------------------------------------------------------------------------------------------------
			
				C#:
			
		
		
		using UnityEngine;
public class PlayerSpawnScript : MonoBehaviour
{
    void Start()
    {
        Instantiate(MenuManager.instance.currentCharacter.prefab, transform.position, Quaternion.identity);
    }
}
	//THIS SCRIPT IS ATTACHED TO THE MAIN CAMERA IN PLAY MODE.
----------------------------------------------------------------------------------------------------------------
			
				C#:
			
		
		
		using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
    public PlayerController thePlayer;
    public Vector3 lastPlayerPosition;
    public float distanceToMove;
    void Start()
    {
        thePlayer = FindObjectOfType<PlayerController>();
        lastPlayerPosition = thePlayer.transform.position;
    }
    void Update()
    {
        distanceToMove = thePlayer.transform.position.x - lastPlayerPosition.x;
        transform.position = new Vector3(transform.position.x + distanceToMove, transform.position.y, transform.position.z);
        lastPlayerPosition = thePlayer.transform.position;
    }
}
	
			
				Last edited by a moderator: