# How to find the 2D box collider center line (xy)?

I want the center of the four sensors to detect only the center of the collider of the lines. Sometimes the player goes off the line.

The sensor’s code:

``````void OnTriggerStay2D (Collider2D coll2D)
{
if (coll2D.gameObject.name == "LinesX") {
LineRight = true;
print ("Right = true");
}
}

void OnTriggerExit2D (Collider2D coll2D)
{
if (coll2D.gameObject.name == "LinesY") {
LineRight = true;
} else {
LineRight = false;
print ("Right = false");
}
}
``````

}

@Kongam03 I have added onto the collision script I gave you in your last post:
this script basically only lets it change lines if the player’s transform is in a range between the centre of the line and then it sets the transform to the centre of the line. because the range between the centre of the line is so small, you don’t notice the player’s transform being set. keep in mind though that this system does stop the player moving in the intersection of 2 lines if they are pressing a left or right key and a up or down key at the same time. I hope you didn’t write too much of your script @rage_co.

`````` PlayerScript playerScript;
GameObject player;
float difference;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
playerScript = player.GetComponent<PlayerScript>();
difference = 0.05f;
}

// Update is called once per frame
private void OnTriggerStay2D(Collider2D collision)
{
print(collision.gameObject.name);
if (player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference)
{
print("X is equal");
}
if(player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference && gameObject.name == "Top")
{
playerScript.top = true;
if (Input.GetKey(KeyCode.UpArrow))
player.transform.position = new Vector2(collision.transform.position.x, player.transform.position.y);
}
if (player.transform.position.x <= collision.transform.position.x + difference && player.transform.position.x >= collision.transform.position.x - difference && gameObject.name == "Bottom")
{
playerScript.bottom = true;
if (Input.GetKey(KeyCode.DownArrow))
player.transform.position = new Vector2(collision.transform.position.x, player.transform.position.y);
}
if (player.transform.position.y <= collision.transform.position.y + difference && player.transform.position.y >= collision.transform.position.y - difference && gameObject.name == "Left")
{
playerScript.left = true;
if (Input.GetKey(KeyCode.LeftArrow))
player.transform.position = new Vector2(player.transform.position.x, collision.transform.position.y);
}
if (player.transform.position.y <= collision.transform.position.y + difference && player.transform.position.y >= collision.transform.position.y - difference && gameObject.name == "Right")
{
playerScript.right = true;
if(Input.GetKey(KeyCode.RightArrow))
player.transform.position = new Vector2(player.transform.position.x, collision.transform.position.y);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (gameObject.name == "Top")
{
playerScript.top = false;
}
if (gameObject.name == "Bottom")
{
playerScript.bottom = false;
}
if (gameObject.name == "Left")
{
playerScript.left = false;
}
if (gameObject.name == "Right")
{
playerScript.right = false;
}
}
``````

I’m sorry if you’ve changed the script at all but i thought it would be easier to solve if i just changed something i had already created.
Hope this helps.