Hey there,
I am like an absolute beginner, so please bear with me, if this question is really stupid. But I can’t figure it out by myself.
I am (trying to) developing a 2D Mobile Game. The catch is that the way you move your character constantly changes from level to level. To make that possible I want to create areas and when you click them, a certain thing happens (like player moving to the left). I could obviously just use buttons to do so, but the way you interact with these areas to get them to function also changes (it could be click, click and hold, click repeatedly, drag/swipe, you get the idea…). Also the area changes position and size in the levels (sometimes the area to move left is on the left side, sometimes it’s on the right side, sometimes it’s the whole left side, sometimes it’s just a tiny space, whatever).
Now, the way I wanted to achieve that is that I created two panels on a canvas (because I want the areas to scale with the screen) and get their positions when the level is started like so:
RectTransform lefti = leftMovement.GetComponent<RectTransform>();
leftleft = leftMovement.transform.position.x;
leftright = leftMovement.transform.position.x + lefti.rect.width;
leftbottom = leftMovement.transform.position.y;
lefttop = leftMovement.transform.position.y + lefti.rect.height;
(I have never inserted code into a forum before, idk if thats how you do it lol)
And then I check whenever the screen is clicked, if the coordinates are within this area like so:
if (screenPos.x > leftleft && screenPos.x < leftright && screenPos.y > leftbottom && screenPos.y < lefttop)
{
playerScript.moveLeft();
}
Technically this works, but also it doesn’t, and here is where I need your help. The values I get back from the panels do not match the real screen values. This is what I receive from the left panel that covers the full left side of the screen with a resolution of 2160x1080:
leftleft: 340 leftright: 740 leftbottom: 1080 lefttop: 2680
UnityEngine.Debug:Log (object)
MMTap:Awake () (at Assets/Code/MoveManager/MMTap.cs:69)
in this case, leftleft and leftbottom should be 0, but they are not. I tried to fix it, by just taking away 340 and 1080 to all the variables and it works when it’s static, but as soon as I change the size of the panels (which is the whole reason I am doing this) the numbers don’t match again. Same thing applies when I change resolution.
I originally worked with
leftleft = Camera.main.WorldToScreenPoint(leftMovement.transform.position).x;
but that gave me real funky values that didn’t match the feedback I got from the InputAction at all.
Please help me ._____. (or let me know that the way I am approaching this problem is stupid in itself and I should just do it differently all together)
thx