这只是我想要做的一件简单的事情。我有我的立方体游戏对象旋转,我想这样做,当相机看到立方体,它停止旋转。如果你能引导我朝着正确的方向前进,我会很感激。谢谢
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();
}
}
}
答案 0 :(得分:2)
实施OnBecameVisible
和OnBecameInvisible
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)
https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
您可以在更新方法中尝试使用.isVisible bool。
这是一个包含其他建议的主题:
http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html