- Home /
How to Position the object perfectly with the Screen Bounds
I am Creating a Pong Like Game (But not exactly the Pong, with a bit of twist) , i am spawning the paddles from the scrip, i want to them to align exactly pixel perfect to the screen edges, with no space , but when i just put collider size as postion , it only shows the half of paddle, but when i subtract the half of size of paddle , it show some extra space, can some help me to align the paddles pixel perfecet, here is my code of what i am doing:
The Frame Creater Class : (it creates frame based on Screen Size or Camera size) : -----
 public class FrameManager : MonoBehaviour
 {
    public Camera camera2d;
    [HideInInspector] public GameObject Wall;
    [HideInInspector] public GameObject Floor;
 
    private void Start() {
        CreateFrame();
    }
 
    private void CreateFrame() {
 
        Vector3 ScreenBounds = camera2d.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,camera2d.transform.position.z));
 
         Wall = new GameObject("Wall") as GameObject;
         Floor = new GameObject("Frame") as GameObject;
 
        // adding colliders for wall and floor
        Wall.AddComponent<BoxCollider2D>();
        Floor.AddComponent<BoxCollider2D>();
 
     //    Change the height and width of Colliders
        Wall.GetComponent<BoxCollider2D>().size = new Vector2(ScreenBounds.x * 2 , ScreenBounds.y * 10);
        Floor.GetComponent<BoxCollider2D>().size = new Vector2(ScreenBounds.x * 10 , ScreenBounds.y * 2);
 
        // making them child of frame object
        Wall.transform.parent = FindObjectOfType<FrameManager>().transform;
        Floor.transform.parent = FindObjectOfType<FrameManager>().transform;
 
     // assigning layer to them
        Wall.layer = 6;
        Floor.layer = 7;
 
        
    }
 }
The Paddle Class : (Responsible for Controlling the paddle and Positioning it) :
 public class Paddles : MonoBehaviour
 {
     private  GameObject Wall;
     private  GameObject Floor;
     private  GameObject PaddleFloor;
     private  GameObject PaddleWall;
 
     public Sprite PaddleSprite;
 
     private void Start() {
         CreateRoom();
         CreatePaddles();
 
     }
 
     private void CreatePaddles() {
         PaddleFloor = new GameObject("Paddle Floor");
         PaddleWall = new GameObject("Paddle Wall");
         PaddleFloor.transform.parent = FindObjectOfType<Paddles>().transform;
         PaddleWall.transform.parent = FindObjectOfType<Paddles>().transform;
 
 
         float posX = Wall.GetComponent<BoxCollider2D>().size.x / 2 ;
         float posY = Floor.GetComponent<BoxCollider2D>().size.y / 2;
 
         PaddleWall.AddComponent<SpriteRenderer>();
         PaddleFloor.AddComponent<SpriteRenderer>();
 
         PaddleWall.GetComponent<SpriteRenderer>().sprite = PaddleSprite; 
         PaddleFloor.GetComponent<SpriteRenderer>().sprite = PaddleSprite;
 
         PaddleFloor.AddComponent<BoxCollider2D>();
         PaddleWall.AddComponent<BoxCollider2D>();
 
         PaddleWall.transform.rotation = Quaternion.Euler(0,0,90);
         
         PaddleFloor.transform.position = new Vector3( 0 , posY - transform.localScale.y / 2 , -10);
         PaddleWall.transform.position = new Vector3( posX - transform.localScale.x / 2  , 0 , -10);
 
     }
 
     private void CreateRoom() {
         Wall = FindObjectOfType<FrameManager>().transform.GetChild(0).transform.gameObject;
         Floor  = FindObjectOfType<FrameManager>().transform.GetChild(1).transform.gameObject;
     }
 
     
 
 }
// This is Image before subtracting the half of local scale :  this is before subtracting the scale :
 this is before subtracting the scale : 
 float posX = Wall.GetComponent<BoxCollider2D>().size.x / 2 ;
         float posY = Floor.GetComponent<BoxCollider2D>().size.y / 2;
         
         PaddleFloor.transform.position = new Vector3( 0 , posY , -10);
         PaddleWall.transform.position = new Vector3( posX , 0 , -10);
// This image is after after subtracting the half of local scale :  I did this for subtracting the half of local scale :
 I did this for subtracting the half of local scale : 
 float posX = Wall.GetComponent<BoxCollider2D>().size.x / 2 ;
         float posY = Floor.GetComponent<BoxCollider2D>().size.y / 2;
         
         PaddleFloor.transform.position = new Vector3( 0 , posY - transform.localScale.y / 2  , -10);
         PaddleWall.transform.position = new Vector3( posX - transform.localScale.x / 2   , 0 , -10);
So can one One help me to align the paddles pixel perfect to the screen bounds, i don't want to hard code the value, can any one help me find the logic
Your answer
 
 
             Follow this Question
Related Questions
C# move y position of object not working 2 Answers
How to place the GameObjects in a sequence on the plane 1 Answer
Syncing localScale 0 Answers
Simultaneously moving GameObjects using C# script. 3 Answers
Multiple Cars not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                