- Home /
Question by
umerufm · Nov 28, 2013 at 01:03 PM ·
2d gameboundingbox
Box boundary 2D Unity3D 4.3
I'm developing snooker like game. So I want to develop Boundary box.One Idea that's in my mind is creating 4 GameObjects and use theme for each side. If you have any other good solution please share it. Thanks in advance
Comment
Best Answer
Answer by umerufm · Mar 08, 2014 at 10:27 PM
I have found best code for it.
public class GameSetup : MonoBehaviour
{
public Camera mainCam = new Camera();
//Reference the colliders we are going to adjust
public BoxCollider2D topWall = new BoxCollider2D();
public BoxCollider2D bottomWall = new BoxCollider2D();
public BoxCollider2D leftWall = new BoxCollider2D();
public BoxCollider2D rightWall = new BoxCollider2D();
// Use this for initialization
void Start ()
{
//Move each wall to its edge location:
topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);
bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
}
}
Thanks Brackeys ;)
Answer by izzycoding · Nov 28, 2013 at 03:16 PM
I am not sure but the edge collider might help here (Although I have not used it yet).
Alternatively you could have an image for your bounding area make the center area transparent and apply a polygon collider and edit its verticies so that the center area is not part of the collider.