when creating multiple prefab lockers, I can only hide in the one that was created first, and when im hiding in another ones, it gives this strange glitch
Here’s the video:
Here’s the code, it’s applied to the triggerbox inside locker model:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LockerHide : MonoBehaviour
{
public Transform cameraHidePos;
public Transform cameraOrigPos;
public Transform camera;
public GameObject player;
public AudioSource heartbeatSound;
public bool isHiding = false;
private bool canHide = false;
void Start()
{
cameraOrigPos = GameObject.Find("OrigPos").transform;
camera = GameObject.Find("FirstPersonCharacter").transform;
player = GameObject.Find("Player");
}
void OnTriggerEnter(Collider col)
{
if(col.transform.tag == "Player")
{
canHide = true;
Debug.Log("can hide");
}
}
void OnTriggerExit(Collider col)
{
if(col.transform.tag == "Player")
{
canHide = false;
Debug.Log("cannot hide");
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.E) && canHide)
{
isHiding = !isHiding;
}
if(isHiding)
{
camera.parent = cameraHidePos;
player.SetActive(false);
camera.position = cameraHidePos.position;
camera.rotation = Quaternion.Euler(180, 0, 180);
heartbeatSound.GetComponent<AudioSource>().enabled = true;
}else
{
camera.parent = player.transform;
player.SetActive(true);
camera.position = cameraOrigPos.position;
heartbeatSound.GetComponent<AudioSource>().enabled = false;
}
}
}