- Home /
Script executing through walls
I've created a script that has the intent of, when the player sees the object, after ~1 second the object will disappear. I'm having a problem that if the player looks in the direction of the object through walls, the script will execute and the object will be gone by the time the player turns to corner to see it.
SCRIPT:
void OnBecameVisible()
{
StartCoroutine(ExampleCoroutine());
}
IEnumerator ExampleCoroutine()
{
yield return new WaitForSeconds(2);
Destroy(gameObject);
}
(Side note: I'm new to game development and scripting so sorry for any mistakes and or me not understanding a reply.)
Answer by Pangamini · May 22, 2021 at 06:26 PM
Try using a raycast between the camera and the object to check if you can see it. It's not perfect, because you are checking for a single point of the object, not its whole visible area, also, using colliders instead of renderers. OnBecameVisible() calls are based on culling, not actual visibility of the object by the player
Your answer
Follow this Question
Related Questions
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
Help me with this code proplem,,Almost finish my game please help this once 3 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer