- Home /
OnBecomeVisible is not working the way i want?
I am working on a horror game project. What i want to do is killing player if enemy is visible and close to player. But on OnbecomeVisible is not working if enemies all body is not in frame. If i can't see enemies foot onbecomevisible doesn't work. How can i fix this. I want this death screen to work if a certain part of the enemy is visible too.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeathHandler : MonoBehaviour
{
//config params
[SerializeField] Canvas DeathUI;
[SerializeField] GameObject AnimatedImage;
[SerializeField] GameObject DeathCam;
[SerializeField] GameObject Player;
[SerializeField] GameObject StaticSound;
[SerializeField] float DeathDistance = 2.1f;
//params
float DistanceBetweenEnemy;
// Start is called before the first frame update
void Start()
{
DeathUI.enabled = false;
}
// Update is called once per frame
void Update()
{
CheckforDistance();
}
IEnumerator DeathScreen()
{
AnimatedImage.SetActive(true);
Player.SetActive(false);
DeathCam.SetActive(true);
StaticSound.SetActive(true);
StaticSound.GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(4.0f);
StaticSound.GetComponent<AudioSource>().Stop();
HandleDeath();
}
public void HandleDeath() //used as string reference
{
Time.timeScale = 0;
DeathUI.enabled = true;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
private void CheckforDistance()
{
DistanceBetweenEnemy= Vector3.Distance(FindObjectOfType<EnemyAI>().transform.position, transform.position);
if(DistanceBetweenEnemy<DeathDistance && FindObjectOfType<EnemySpawner>().isEnemyVisible)
{
StartCoroutine(DeathScreen());
}
}
`
Your answer
Follow this Question
Related Questions
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
How to properly use Renderer.OnBecameInvisible for Culling objects off screen? 2 Answers
Is it reasonable to use OnBecameVisible for controlling animations, lights and particle systems? 1 Answer
why the function OnBecameVisible and OnBecameInvisible doesn't work in canvas renderer ? 1 Answer
Is there a way to trigger OnBecameVisible when invisible 1 Answer