- Home /
Render.isVisible help
I created a script which is turned on after an object is set active. When the object is set active you have a certain amount of time to find and look at the object or it will attack you. If you are able to look at the object in the given time, the script is disabled and another one is enabled to set the object active. I used a if(GetComponenet().isVisible) and added a debug logs to see if it works.
{
public MonoBehaviour othercomponent;
private bool shown;
RaycastHit hit;
public float thedistance;
public int speed;
Coroutine co;
// Use this for initialization
void Start() {
StartCoroutine(Startt());
co = StartCoroutine(Startt());
}
IEnumerator Startt()
{
Showing();
shown = true;
yield return new WaitForSeconds(speed);
shown = false;
Attacking();
}
IEnumerator Startt2()
{
yield return new WaitForSeconds(speed);
othercomponent.enabled = true;
GetComponent<Shown>().enabled = false;
}
// Update is called once per frame
void Showing()
{
Debug.Log("Shown");
if (GetComponent<Renderer>().isVisible)
{
Debug.Log("Visible");
thedistance = hit.distance;
StartCoroutine(Startt2());
StopCoroutine(co);
}
}
void Attacking()
{
if (shown == false)
{
Debug.Log("Attacck");
}
}
void Update()
{
}
}
The Problem is that the object does not become visible even when both scene and game view are looking at it. Am I using the isVisible wrong?
isVisible in my experience is not accurate enough to use for game play. It's more for rendering proficiency in large levels to limit sending mesh info to the video card and does have some inaccuracy.
depending on your game and how many objects you have... i would recommend using a very occasional general distance check. if you use square distance its much more proficient than unity's Vector3 distance. (just subtract start and finish vector points to see if the absoulutes are both smaller than your liking).
sounds like you could limit to every second or so with your checks.
once you know an object is within proximity you could compair your players forward axis to the angle between the player and object using something like this: [https://docs.unity3d.com/ScriptReference/Vector3.Angle.html][1]
do an occational raycast to the object to see if you have a hit (probobly from the top).
Answer by MarioSantoso · Apr 15, 2018 at 03:22 AM
You should use these to check whether an object is visible to a camera or not
private void OnBecameVisible()
{
// your code here
}
private void OnBecameInvisible()
{
// your code here
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612160801im_/https://answers.unity.com/themes/thub/images/avi.jpg)