调用某个脚本时,玩家相机不会被停用

问题描述 投票:0回答:1

我目前正在制作一个小项目,玩家可以隐藏在储物柜和桌子下面。我知道,当玩家隐藏在储物柜中时,玩家对象会自动消失,但每当我与附有脚本的桌子进行交互时。它似乎并没有消失。相反,它只是卡住了,这真的很烦人,因为我试图在隐藏在桌子下面时将玩家移走。有人能帮我解决这个问题吗?我到处都在寻找如何解决这个问题,我不认为问题依赖于代码本身。但我只是不得不四处询问。

这是该表的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerTable : MonoBehaviour
{
    [SerializeField] private bool isHidingTable;
    [SerializeField] private bool canHideTable;
    [SerializeField] private Transform cameraTablePos;
    [SerializeField] private new Transform camera;
    [SerializeField] private GameObject player;
    [SerializeField] private Camera playerCamera;
    [SerializeField] private Camera tableCamera;

    void Start()
    {
        cameraTablePos = GameObject.Find("TablePosition").transform;
        player = GameObject.Find("Player");
        
        playerCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
        tableCamera = GameObject.Find("TableCamera").GetComponent<Camera>();

        playerCamera.enabled = true;
        tableCamera.enabled = false;
        
        tableCamera.transform.SetParent(null);
    }

    void switchToTableCam()
    {
        player.SetActive(false);
        playerCamera.enabled = false;
        tableCamera.enabled = true;
    }

    void switchToPlayerCam()
    {
        player.SetActive(true);
        tableCamera.enabled = false;
        playerCamera.enabled = true;
    }

    private void OnTriggerEnter(Collider collision)
    {
        if (collision.CompareTag("Player"))
        {
            canHideTable = true;
        }
    }

    private void OnTriggerExit(Collider collision)
    {
        if (collision.CompareTag("Player"))
        {
            canHideTable = false;   
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && canHideTable)
        {
            isHidingTable = !isHidingTable;
        }

        if (isHidingTable)
        {
            tableCamera.transform.SetParent(null);
            camera.parent = cameraTablePos;
            camera.position = cameraTablePos.position;
            switchToTableCam();
        }

        else
        {
            switchToPlayerCam();
        }
        
    }
}

这是储物柜的脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class TriggerLockerDoor : MonoBehaviour
{
   [SerializeField] private bool isHidingLocker;
   [SerializeField] private bool canHideLocker;
   [SerializeField] private Transform cameraLockerPos;
   [SerializeField] private new Transform camera;
   [SerializeField] private GameObject player;
   [SerializeField] private Camera playerCamera;
   [SerializeField] private Camera lockerCamera;

   void Start()
   {
      cameraLockerPos = GameObject.Find("LockerPosition").transform;
      player = GameObject.Find("Player");
      
      playerCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
      lockerCamera = GameObject.Find("LockerCamera").GetComponent<Camera>();    
      
      playerCamera.enabled = true;
      lockerCamera.enabled = false;
      
      lockerCamera.transform.SetParent(null);
   }

   void switchToLockerCam()
   {
      playerCamera.enabled = false;
      lockerCamera.enabled = true;
   }
   
   private void OnTriggerEnter(Collider collision)  
   {
      if (collision.CompareTag("Player"))
      {
         canHideLocker = true;
      }
   }
   
   private void OnTriggerExit(Collider collision)
   {
      if (collision.CompareTag("Player"))
      {
         canHideLocker = false;
      }
   }

   void Update()
   {
      if (Input.GetKeyDown(KeyCode.E) && canHideLocker)
      {
         isHidingLocker = !isHidingLocker;
      }

      if (isHidingLocker)
      {
         lockerCamera.transform.SetParent(null);
         camera.parent = cameraLockerPos;
         player.SetActive(false);
         camera.position = cameraLockerPos.position;
         switchToLockerCam();
      }

      else
      {
         player.SetActive(true);
         lockerCamera.enabled = false;
         playerCamera.enabled = true;
      }
   }
   
}

这是我的游戏空间和我面临的问题类型的视频。

[链接到我的游戏空间视频1

c# unity-game-engine
1个回答
0
投票

您正在将播放器相机设置为启用每一帧。您应该仅在开始或结束隐藏时更改它。你的储物柜和你的桌子每一帧都在运行,而且你的储物柜似乎是在之后运行的,所以它会覆盖桌子所做的任何更改。

另外,有什么理由不为两者使用一个脚本?

无论如何,这看起来更简单:

public class HidingPlace : MonoBehaviour
{
    [SerializeField] private Camera hidingCamera;
    [SerializeField] private GameObject player;
    [SerializeField] private string playerTag = "Player";
    
    private Camera playerCamera; // Could also expose this with [SerializeField]
    private bool canHide;
    private bool isHiding;

    private void Awake()
    {
        // We are assuming the camera is a child of the player object
        playerCamera = player.GetComponentInChildren<Camera>();
        hidingCamera.enabled = false;
    }
    
    private void OnTriggerEnter(Collider collider)
    {
        // Only change canHide when we are walking around, not when already hiding
        if (isHiding) return;
        
        if (collider.CompareTag(playerTag))
        {
            canHide = true;
        }
    }
    
    private void OnTriggerExit(Collider collider)
    {
        // Only change canHide when we are walking around, not when already hiding
        if (isHiding) return;
        
        if (collider.CompareTag(playerTag))
        {
            canHide = false;
        }
    }
    
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && canHide)
        {
            hiding = !hiding;
            if (hiding)
            {
                player.SetActive(false);
                hidingCamera.enabled = true;
            }
            else
            {
                player.SetActive(true);
                hidingCamera.enabled = false;
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.