簡體   English   中英

Unity-如何在相機看到運動物體時停止運動物體

[英]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();
    }
}

}

實現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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM