79179318

Date: 2024-11-11 22:34:35
Score: 0.5
Natty:
Report link

You are setting the player camera to enabled every frame. You should only change it when you start or end hiding. Your locker and your table are running every frame, and it seems that you locker is running afterwards, so it's overriding any changes that the table made.

Also, is there any reason why you aren't just using a single script for both?

This seems much simpler anyways:

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;
            }
        }
    }
}
Reasons:
  • Blacklisted phrase (1): is there any
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Alex Davies