Unity - 如何在相机看到移动对象时停止移动对象

时间:2017-09-20 20:41:55

标签: unity3d camera renderer gameobject

这只是我想要做的一件简单的事情。我有我的立方体游戏对象旋转,我想这样做,当相机看到立方体,它停止旋转。如果你能引导我朝着正确的方向前进,我会很感激。谢谢

public class cubeMove : MonoBehaviour, MoveObject
{
public Renderer rend;

    public void Update () {

    rend = GetComponent<Renderer>();
    stopWhenSeen();      
}
public void move()
{
    transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
public void stopWhenSeen()
{
    if (rend.enabled == false)
    {
        move();
    }
}

}

2 个答案:

答案 0 :(得分:2)

实施OnBecameVisibleOnBecameInvisible MonoBehaviour的消息:

private visible = false ;

// OnBecameVisible is called when the renderer became visible by any camera. INCLUDING YOUR EDITOR CAMERA
void OnBecameVisible()
{
    visible = true ;
}

// OnBecameInvisible is called when the renderer is no longer visible by any camera. INCLUDING YOUR EDITOR CAMERA
void OnBecameInvisible()
{
    visible = false;
}

void Update()
{
    if( !visible )
        move() ;
}

public void move()
{
    transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}

答案 1 :(得分:0)