Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Detecting if I entered from the sides or the Top/bottom of a square.

Discussion in 'Physics' started by Kris_VRS, Dec 6, 2022.

  1. Kris_VRS

    Kris_VRS

    Joined:
    Jun 3, 2022
    Posts:
    1
    Using a collider Trigger OnTriggerEnter2D
    what I did, was to calculate the squares.bounds against the player.position as so:

    int PositionX = (int)(Mathf.Abs((player.position.x - square.bounds.min.x) + (player.position.x - square.bounds.max.x) + (player.position.x - square.bounds.center.x)));

    int PositionY= (int)(Mathf.Abs((player.position.y - square.bounds.min.y) + (player.position.y - square.bounds.max.y) + (player.position.y - square.bounds.center.y)));

    The higher value between PosX and Pos Y will always return if I entered from North/South or East/West from the center of the Square position.

    It works...but I don't think it's "pretty" and believe it might give me a bad time later down the line, since it ONLY works if the shape is perfectly Square.

    I was wondering if there was a better way of doing this or if Unity had a built in fonction/method that did it?
    I know I could be using buffer, or dedicated zone that knows(by hard coding )which way you should have entered, but I want to set it up with logic in coding.