I’m trying to have an enemy in my game disappear after being looked at.
The script itself is basic right now, and during testing I ran into a problem: the object is still ‘visible’ when behind a wall I made. I understand that the engine still has to render the object behind the wall, but I’m wondering if theirs a quick fix for this.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
public class eye_reciever : MonoBehaviour
{
public float vis_time = 2f;
bool shy;
float start;
// Start is called before the first frame update
void Start()
{
start = Random.Range(0, 1);
if (start != 0)
{
shy = false;
UnityEngine.Debug.Log("I'm not shy from begining!");
}
else
{
shy = true;
UnityEngine.Debug.Log("I'm shy from beginning!");
}
}
// Update is called once per frame
void Update()
{
if (GetComponent<Renderer>().isVisible == true && shy == false)
{
vis_time -= Time.deltaTime;
if(vis_time <= 0)
{
shy = true;
UnityEngine.Debug.Log("I'm shy from vis_time = 0!");
GetComponent<Renderer>().enabled = false;
GetComponent<BoxCollider>().enabled = false;
// GetComponent<Transform>().localPosition = Vector3.zero; [Get it to teleport, either randomly or at set locations. Idk]
}
}
else if(GetComponent<Renderer>().isVisible == false && shy == false)
{
vis_time += Time.deltaTime;
}
if (GetComponent<Renderer>().enabled == false)
{
vis_time += Time.deltaTime;
}
if (vis_time >= 4f)
{
shy = false;
UnityEngine.Debug.Log("I'm not shy from vis_time replenishing!");
GetComponent<Renderer>().enabled = true;
GetComponent<BoxCollider>().enabled = true;
}
}
}
Any help would be greatly appreciated!