- Home /
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");
}
}
}
I don't know exactly but because the object's origin is situated in the middle of it......and the box collider is applied uniformly on it....maybe you can use the transform values to achieve the intended effect
Like @rage_co, you could get the axis of the line, For example:
X(Vertical) and Y(Horizontal)
and when you detect a collision, you get the X or Y axis of the line and set your player's X or Y axis to that value
maybe something along the lines of checking and storing the position of the object every frame and the last frame and if it's less than the x value of the sensor's transform in the last frame and equal to or more than that in the current one....call the desired funtion....same if it's more than that value in the last frame and equal or less than that in the current frame....and the same can be carried onto the y value....might get a lot more complicated by adding a third axis but for a 2d game like yours, this should work just fine...and should not present problems like missing the collision too
Answer by Anonymous620 · Jul 18, 2021 at 04:48 AM
@Kongam03 I have added onto the collision script I gave you in your last post: https://answers.unity.com/questions/1847116/sliding-movement-script-in-2d-doesnt-stop.html?childToView=1847642#comment-1847642 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.
hmmm...i did not understand what the question actually was.....but i suppose my method was too complicated since i had thought of it as something for free movement (it was a bit unnecessary for even that)......so your script is perfect i think.....and don't worry...i didn't get much time to make anything today so i hadn't even started
I'm glad you didn't waste time on it. It was much easier for me as I already had the scene and script from his last post setup and experienced the same problem. I can see why you didn't understand the question. Also I have all day because it's school holidays.
if that helped you, please accept the answer and help lessen the load on the site, plus this will make it easy for others to find an answer
Your answer
![](https://koobas.hobune.stream/wayback/20220613040404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Black lines after using PackTextures() 1 Answer
Glitchy shadow lines: Toon Lighted (screenshot) 0 Answers
Change Z offset of lines 1 Answer
how to quickly disable vertices from rendering in a Mesh? 1 Answer
'center' is not a member of 'Object' 0 Answers