Deciding where in relation to the player a touch is

Ok, so I will try to explain this simply and hope it makes sense.

I have a player and I want different things to happen depending on where in relation to the player the screen is pressed. In basic terms for example - forward is shoot, backwards is boost, down is jump. But the decision is not made on a simple plane.

For example I only want the shoot to occur if the press is in a 70degree arc in front of the player, jump in 70degree arc down and boost in any other space. I am also trying to write it so that it can be expanded which means I need to be as clever as possible with the decision.

In a basic form( only in my head at the moment)

  • Get the player position and touch
  • Zero the player position(TouchPos -
  • Drawn lines out from the player /
    Create a class for that line
    (attributes = xGradiant, YGradiant,
    moveForOneSide, moveForOtherSide)
  • Perform some tests to find the right
    line to check on, is the touch in
    NorthEast, SouthEast, SouthWest,
    SouthEast zone.
  • Find out which line is in that zone
  • Find the touch/line intersection on
    the X axis then see if the Y axis
    (using x * yGradiant) is oneSide(>)
    otherSide(<) and I have my move to

I know that there is probably a much better way to find this out with Maths so hopefully someone can help as I feel like my method is very inefficient and feels clunky… any help appreciated, if this doesn’t make enough sense let me know and I can draw it out. even just pointing me in the direction of how this can be calculated on paper I can go off an do some learning.


I figured out I can use this equation to just find the angle of the click in a circle related to the players position, then I can just set a range of angles for each move, this will return 0-360degrees with 12:00 being 0:

        Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
        Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);

        GetAngleFromPoint(target, playerPosition);

internal double GetAngleFromPoint(Vector2 TouchPoint, Vector2 PlayerPos)
    double dy = (TouchPoint.y - PlayerPos.y);
    double dx = (TouchPoint.x - PlayerPos.x);

    double theta = Math.Atan2(dy, dx);

    double angle = (90 - ((theta * 180) / Math.PI)) % 360;

    return angle;