Search Unity

Getting scene-relative Cinemachine vcams to follow one player between scenes

Discussion in '2D' started by lemnsic8, Aug 11, 2019.

  1. lemnsic8

    lemnsic8

    Joined:
    Aug 10, 2019
    Posts:
    1
    I'm currently creating a 2D RPG and have a camera following the player, who is not destroyed between scenes. However, the pre-existing vcam isn't deleting itself upon transferring to a specific scene. i want the vcam in the next scene to delete itself when the player's vcam enters, but I want it so that I can play any scene and achieve the same results.

    Camera Controller Script, attached to the Main Camera:

    Code (CSharp):
    1. /
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using Cinemachine;
    7. using System.Collections.Specialized;
    8. using System.Security.Cryptography;
    9.  
    10. public class CameraController : PlayerController
    11. {
    12.  
    13.     //public CinemachineVirtualCamera vcam;
    14.     public PlayerController player;
    15.     static CameraController MainCamera;
    16.  
    17.     void Awake()
    18.     {
    19.         //vcam = GetComponent<CinemachineVirtualCamera>();
    20.         DontDestroyOnLoad(GameObject.Find("CM vcam1"));
    21.      
    22.     }
    23.  
    24.     // Start is called before the first frame update
    25.     void Start ()
    26.     {
    27.         /*camPos.Set(playerPos.position.x, playerPos.position.y, 0f);
    28.         vcam.Follow.transform.position = camPos; */
    29.  
    30.         //we want to be a singleton. this means that there should only ever be one player instance at any given time.  
    31.  
    32.         if (MainCamera != null)
    33.         {
    34.             //another program is the singleton
    35.             //so destroy this one
    36.             UnityEngine.Debug.Log("Pre-Existing Camera was destroyed");
    37.             Destroy(vcam);
    38.             Destroy(this.gameObject);
    39.             return;
    40.         }
    41.  
    42.         //if the script comes here, this is the singleton
    43.         MainCamera = this;
    44.         GameObject.DontDestroyOnLoad(this.gameObject); //become immortal (this.gameObject does nothing different from gameObject)
    45.  
    46.  
    47.         setCamFollow();
    48.     }
    49. }
    50.  

    Player Controller:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Diagnostics;
    6. using System.Security.Cryptography;
    7. using System.Threading;
    8. using UnityEngine;
    9. using Cinemachine;
    10.  
    11. public class PlayerController : MonoBehaviour
    12. {
    13.     public float moveSpeed;
    14.  
    15.     private Animator anim;
    16.     public Rigidbody2D myRigidbody;
    17.    
    18.  
    19.     private bool playerMoving;
    20.     public Vector2 lastMove;
    21.  
    22.     public Vector2 startDirection;
    23.     public string startPoint;
    24.  
    25.     private static bool playerExists;
    26.     private static GameObject instance;
    27.  
    28.     protected Vector3 playerPos;
    29.     public CinemachineVirtualCamera vcam;
    30.  
    31.     static PlayerController MainPlayer;
    32.  
    33.     void Awake()
    34.     {
    35.         anim = GetComponent<Animator>();
    36.         myRigidbody = GetComponent<Rigidbody2D>();
    37.  
    38.         vcam = GetComponent<CinemachineVirtualCamera>();
    39.     }
    40.    
    41.  
    42.     // Start is called before the first frame update
    43.     void Start()
    44.     {
    45.        
    46.  
    47.         //we want to be a singleton. this means that there should only ever be one player instance at any given time.  
    48.  
    49.         if (MainPlayer != null)
    50.         {
    51.             //another program is the singleton
    52.             //so destroy this one
    53.             UnityEngine.Debug.Log("Player was destroyed");
    54.             Destroy(this.gameObject);
    55.             return;
    56.         }
    57.  
    58.         //if the script comes here, this is the singleton
    59.         MainPlayer = this;
    60.         GameObject.DontDestroyOnLoad(this.gameObject); //become immortal (this.gameObject does nothing different from gameObject)
    61.  
    62.     }
    63.  
    64.     // Update is called once per frame
    65.     void FixedUpdate()
    66.     {
    67.  
    68.         Destroy(GameObject.Find(gameObject.name + " (Clone)"));
    69.  
    70.         playerMoving = false;
    71.        
    72.         // format is:
    73.         //transform.Translate(new Vector3 (x-axis force, y-axis force, z-axis force));
    74.  
    75.         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    76.         {
    77.             transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
    78.             playerMoving = true;
    79.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    80.         }
    81.  
    82.         if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    83.         {
    84.             transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    85.             playerMoving = true;
    86.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    87.         }
    88.  
    89.         anim.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
    90.         anim.SetFloat("moveY", Input.GetAxisRaw("Vertical"));
    91.         anim.SetBool("PlayerMoving", playerMoving);
    92.         anim.SetFloat("lastMoveX", lastMove.x);
    93.         anim.SetFloat("lastMoveY", lastMove.y);
    94.     }
    95.  
    96.     protected void setCamFollow ()
    97.     {
    98.         if (vcam.Follow = null)
    99.         {
    100.             Destroy(vcam);
    101.             vcam.Follow = this.gameObject.transform;
    102.         }
    103.     }
    104. }
    105.  
    106.  
     
  2. holyknight03

    holyknight03

    Joined:
    Jun 25, 2020
    Posts:
    1
    I am having the exact same issue, trying to do the same thing. When I figure it out, I'll post my solution.