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\$
4
  • \$\begingroup\$ Can you show us the coroutine you're using at the moment? Also, in your example, it looks like the cubes have Renderer components, so why wouldn't OnBecameVisible would with those? \$\endgroup\$
    – DMGregory
    Apr 28, 2019 at 14:12
  • 1
    \$\begingroup\$ answers.unity.com/questions/8003/… \$\endgroup\$ Apr 28, 2019 at 15:11
  • \$\begingroup\$ 1-I used the cubes as the basis of my players 2-I use 2 transform before getting to my player for example tranform 1 father, 2 pillar tranform, 3 my player, it would be better to disable the father who is more practical and fast \$\endgroup\$
    – Nitecki
    Apr 28, 2019 at 17:20
  • \$\begingroup\$ Look at GeometryUtility.CalculateFrustumPlanes and GeometryUtility.TestPlanesAABB. \$\endgroup\$ Apr 29, 2019 at 3:24

1 Answer 1

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\$
3
  • \$\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, 2019 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, 2019 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, 2019 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .