權界我有我的遊戲對象的代碼波紋管:試圖保持屏幕內的遊戲對象留在unity3d
private float screenx;
Vector3 playerPosScreen;
void Start() {
screenx=Camera.main.pixelWidth-renderer.bounds.size.x ;
}
void update(){
playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (playerPosScreen.x >= screenx) {
//playerPosScreen.x=screenx;
transform.position=new Vector3 (screenx, transform.position.y,transform.position.z);
}
//txt.text = playerPosScreen.x.ToString();
else if(playerPosScreen.x<=renderer.bounds.size.x){
transform.position=new Vector3 (renderer.bounds.size.x, transform.position.y,transform.position.z);
}
}
我正在開發一個2D
遊戲與orthographic
相機,我的問題是gameobject繼續走出屏幕,我在這裏錯過了什麼?
對於輸入錯誤'update',我感到抱歉,這只是一個輸入錯誤,並且非常感謝您提供關於澄清和代碼的提示。我會測試它:) – Sora
沒問題。至少對我來說,代碼在正交和透視相機上都可以工作。 – maZZZu