- Home /
Limit the available stage space?
I know the title does me no justice, but I'm working on making a side scrolling space shooter game (sure everyone knows the kind). And I was wondering whats the best way to go about locking the ships movement to a set rectangle that's the same as the viewport, how would you guys do this?
Hi, I'm not a pro with camera coords convertion, but you just need to know the distance between the camera and the "space", get the rect and clamp the position consequently.
something along the lines of getting the extents of the screens view, and then comparing the world position of the ship, to the position at the edge (maybe some padding too) and if the players ship passes too far, limit him there at the edge...
so like... screen.width and screen.height will be helping.. and then using camera.main.screenpointtoworldpoint or whatever might help you out as well.
Answer by tomtom789 · Feb 28, 2013 at 04:15 PM
Or You Simply make some Cubes around your scene and make them not visible with colliders. ;)
If your object is attached to your camera you can just check the positions: Exemple:
void Update()
{
if (this.transform.position.x > 15.0f) // left
this.transform.position.x = new Vector3(15.0f, this.transform.position.y, this.transform.position.z);
if (this.transform.position.x < -15.0f) // Right
this.transform.position.x = new Vector3(-15.0f, this.transform.position.y, this.transform.position.z);
if (this.transform.position.y < 15.0f) // Up
this.transform.position.y = new Vector3(this.transform.position.x, 15.0f, this.transform.position.z);
if (this.transform.position.y < -15.0f) // Down
this.transform.position.y = new Vector3(this.transform.position.x, -15.0f, this.transform.position.z);
}
Or a thing like this. And this work only if your objet is attached to your camera.
Hope it helps. ;)
That couldn't work well in various screen sizes, but if you only use one exact size that would be ok. I still think getting screen extents, then converting either world coordinates to screen coordinates or vice versa would be best for any screen size.
Thanks for the quick responses guys! I already tried the cube idea but the collision isn't always clean at the speeds my ship gets up to! But I'll definitely give some of these a try, thanks a ton!
Use cubes as border is not really efficient. Put your elements in camera's children, that's a funny trick.