I am currently working on a simple Space Shooter. To clamp my space ship to the screen I use this code snippet:
ScreenPosition = Camera.main.WorldToViewportPoint (this.transform.position);
ScreenPosition.x = Mathf.Clamp(ScreenPosition.x,0f, 1f);
ScreenPosition.y = Mathf.Clamp(ScreenPosition.y, 0f, 1f);
this.transform.position = Camera.main.ViewportToWorldPoint(ScreenPosition);
But the space ship is invisible when I start the game. It is still shooting, but the ship and its children are not visible. If I delete the last line, everything is fine, except that the ship isn’t clamped to the screen anymore. I can’t figure out how to work around this problem.
edit: I am not getting any errors in the console.
Thanks in advance…