Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rectangles always drawing at the top of the screen

Discussion in '2D' started by krazykhris875, Jul 31, 2014.

  1. krazykhris875

    krazykhris875

    Joined:
    Jul 17, 2013
    Posts:
    9
    Hey,

    So I've just finished a lovely 2D tutorial by Lynda.com, which I highly recommend. I thought a good way to get some extra practice and experience before starting on my own game would be to take the tutorial project, and add touch controls so that it can be played on an Android or iOS device.

    Right now, I've got the game using GUI.Box to render visible buttons, however these are just something for the player to see. The actual controls are handled by simple rectangles, then using the Rect.Contains method. People have said that with rectangles, Unity uses a bottom-left to top-right coordinates system, rather than top-left to bottom-right. This would be fine, but I can't get my rectangles to move in the y axis at all...

    Here's the code:

    public Rect thrustButton = new Rect(0, 0, 200, 200);
    public Rect leftButton = new Rect(Screen.width - 400, 0, 200, 200);
    public Rect rightButton = new Rect(Screen.width - 200, 0, 200, 200);


    if (Input.touchCount > 0) {
    var touchPos = Input.GetTouch (0).position;

    if (thrustButton.Contains (touchPos))
    moving.y = 1;

    if (leftButton.Contains(touchPos))
    moving.x = -1;

    if (rightButton.Contains(touchPos))
    moving.x = 1;

    Now, this works, except that I absolutely CANNOT get the rectangles to draw in the right spot. They're supposed to be along the bottom of the screen, with the thrustButton on the left, and the leftButton and rightButton side by side on the right. There in the correct spot in the x axis, but always at the very top of the screen. I found this by trial and error (dragging my finger around the screen until it hit a button). Now, no matter what I change the y coordinate value to, it never ever moves. 0 should be correct, if it's moving up from the bottom and I want it to draw at the bottom, but it still draws at the top. I've tried changing it to 500, -500, Screen.height, Screen.height - Screen.height, Screen.height + 100, Screen.height - 100. And still, it's always at the exact same spot at the top of the screen. It's incredibly frustrating (especially since I have to compile for Android each time to test it), and I'm absolutely at a loss. Any help would be massively appreciated. Thanks.
     
  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Try writing the Screen.Height into the debug log or to the screen and see what it comes out with.

    It might be thinking its zero or a very low value, which would give you the symptoms you're looking at. Don't ask me why that would happen though, you'll have to figure that out next if its the case :)