(Sorry, maybe my English isn´t perfect)
Hi, I want to play a sound or enable a gameObject if something (a NPC) is visible (In a range), but I have no idea how to do that.
For example: If I see a zombie, which is closer than e.g. 20m a text will appear, and if he isnt visible anymore, the gameObject will be disabled again.
Thank you!
If you want this to be able to be enabled and disabled you’ll need to place a second script which handles this as once you’ve disabled it Update will no longer run.
void Update(){
if(renderer.isVisible && Vector3.Distance(zombie.position, player.position) <= 20f){
showText = true;
}else{
showText = false;
}
}
void OnGUI(){
if(showText){
GUI.Label("Zombie")
}
}
You could use void OnBecameVisible(){} and OnBecauseInVisible(){} to add and remove a delegate function instead of using Update to remove the small overhead though may be more trouble than it’s worth.