[英]Unity-How to stop a moving object when the Camera sees it
這只是我想做的一件簡單的事情。 我讓我的立方體游戲對象旋轉,因此我想使其旋轉,以便當攝像機看到立方體時,它停止旋轉。 如果您能引導我朝正確的方向前進,我將不勝感激。 謝謝
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();
}
}
}
實現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);
}
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.