0
\$\begingroup\$

I want to toggle an object's active state using the camera view:

  • when the object leaves the view it deactivates
  • when the object enters the camera's view it activates

I'm using this to control the activity of objects in a carousel like this

I did this with a coroutine, but in my project the fps is falling a lot because of the coroutine.

I've already used OnBecameVisible and isVisible but that's not it since they only work with a renderer.

    public void next() {
        StartCoroutine(On(waitnext));
    }

    IEnumerator On(float waitnext) {
        yield return new WaitForSeconds(waitnext);
        myArray[Selecao].SetActive(false);
        Selecao++;
        if(Selecao > myArray.Length - 1) {
            Selecao = 0;
        }
        yield return new WaitForSeconds(waitnext);
        myArray[Selecao].SetActive(true);
    }

    public void previous() {
        StartCoroutine(On2(waitprevious));
    }

    IEnumerator On2(float waitprevious) {
        yield return new WaitForSeconds(waitprevious);
        myArray[Selecao].SetActive(false);
        Selecao--;
        if(Selecao < 0) {
            Selecao = myArray.Length - 1;
        }
        yield return new WaitForSeconds(waitprevious);
        myArray[Selecao].SetActive(true);
    }
\$\endgroup\$
0
\$\begingroup\$

You can achieve this by adding colliders to your object and camera.

  • preferably set the camera collider isTrigger on if the object is a rigid body.
  • You can use tags, layers and game object name to filter which object you want to operate on.

Here is the code:

[RequireComponent(typeof(Collider))]
public class FrustomColliderDetector : MonoBehaviour
{
    //Gets called when it enters the camera collider
    private void OnCollisionEnter(Collision other) {
        if(other.gameObject.tag == "object-tag")
            other.gameObject.SetActive(true);
    }
    //Gets called when it leaves the camera collider
    private void OnCollisionExit(Collision other) {
        if(other.gameObject.tag == "object-tag")    
        other.gameObject.SetActive(false);
    }

}
\$\endgroup\$
  • \$\begingroup\$ I even thought about using istrigger, but I do not know if this situation would work, would the collider have to be in the middle or at the edges of the camera view? \$\endgroup\$ – Nitecki Apr 28 '19 at 17:52
  • \$\begingroup\$ To have an accurate result, you have to align the collider with the view frustum which is probably a mesh collider, but you the box collider May work for you as well. \$\endgroup\$ – Emad Apr 28 '19 at 22:35
  • \$\begingroup\$ with certainty (oncollision and the trigger), does not work for my case because if I disable the gameobject it no longer has the reference to try to activate again \$\endgroup\$ – Nitecki Apr 29 '19 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.