Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Switching Camera functions not working

Discussion in 'Scripting' started by TheJumpah, Jan 7, 2020.

  1. TheJumpah

    TheJumpah

    Joined:
    Nov 2, 2019
    Posts:
    6
    Hello, I currently have my camera following my player's movement directly. I also have an option that if my player presses "Y" then the camera will no longer follow the player's movement and the player will be able to pan around the screen instead like league of legends. Both Camera functionalities work fine but switching from one to the other isn't working as well. When I click "Y" the first time the camera will no longer follow my player how I want it but when I press it again it doesn't go back to following the player. I can't seem to understand why. Can anyone give me a helping hand? Thanks in advance!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float smoothSpeed = 0.125f;
    9.     public Vector3 offset;
    10.     CameraFollow cam;
    11.     CameraController unlockedCam;
    12.     [SerializeField]bool unlocked = false;
    13.  
    14.     private void Start()
    15.     {
    16.         cam = GetComponent<CameraFollow>();
    17.         unlockedCam = GetComponent<CameraController>();
    18.         unlockedCam.enabled = false;
    19.     }
    20.     private void LateUpdate()
    21.     {
    22.         transform.position = player.position + offset;
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         if (Input.GetButtonDown("Camera") && unlocked == false)
    28.         {
    29.             cam.enabled = false;
    30.             unlockedCam.enabled = true;
    31.             unlocked = true;
    32.         }
    33.         else if (Input.GetButtonDown("Camera") && unlocked == true)
    34.         {
    35.             cam.enabled = true;
    36.             unlockedCam.enabled = false;
    37.             unlocked = false;
    38.         }
    39.     }
    40.  
    41. }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    "cam" is a reference to the current script that is running. When you disable it, this script stops executing the Update method, so you can't enable it again. Instead of enabling and disabling the script, in late update, you can use
    Code (csharp):
    1. private void LateUpdate()
    2. {
    3.    if(unlocked == false)
    4.       transform.position = player.position + offset;
    5. }
     
  3. TheJumpah

    TheJumpah

    Joined:
    Nov 2, 2019
    Posts:
    6
    Thank you that worked perfectly!