Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JMC17 · Aug 03, 2013 at 12:58 PM · c#guipositionscreen

x0,y0 doesn't always point to the bottom left corner

I'm trying to keep the joystick in the bottom left corner but Screen.width*0.0f and Screen.height*0.0f points to the top left corner while Touch.position shows x==0,y==0 in the bottom left corner.. why? I switched the landscape mode around in case that was it but it didn't work.

It's very important both matches, so I can compare positions and know where exactly it was touched.

Here's my script: (Hopefully not too messy)

 using UnityEngine;
 using System.Collections;
 
 public class J1 : MonoBehaviour {
     public Texture2D[] JoystickImages=null;
         float MEM1,MEM2;
     Vector2 MEM3,MEM4;
     int JOYstandard=999;
     Vector2[] Sectionsa=new Vector2[5],Sectionsb=new Vector2[5];
     const float IncrementsMEM3=0.20f;
     float yValuea=0.99f,xValuea=0.00f,yValueb=0.80f,xValueb=0.20f;
     //    int HALFSIZE=75;//HALF OF 150 DRAWTEXTURE WIDTH HEIGHT
     // Use this for initialization
     void Start () {
     MEM1=JoystickRect.width;
         MEM2=JoystickRect.height;
             MEM3=new Vector2(JoystickRect.x+(MEM1*0.5f),JoystickRect.y+(MEM2*0.5f));
         for (int i=0;i<5;++i){
             Sectionsa[i]=new Vector2(MEM3.x+xValuea,MEM3.y*yValuea);
             Sectionsb[i]=new Vector2(MEM3.x+xValueb,MEM3.y*yValueb);
             yValuea=yValuea-IncrementsMEM3;
             yValueb=yValueb-IncrementsMEM3;
         }
         
     /*
         //COLUMN 1 ROWS                                 
         Col1Section1a=new Vector2(MEM3.x*0.00f,MEM3.y*0.99f);
         Col1Section1b=new Vector2(MEM3.x*0.20f,MEM3.y*0.80f);
         Col1Section2a=new Vector2(MEM3.x*0.00f,MEM3.y*0.79f);
         Col1Section2b=new Vector2(MEM3.x*0.20f,MEM3.y*0.60f);
         Col1Section3a=new Vector2(MEM3.x*0.00f,MEM3.y*0.59f);
         Col1Section3b=new Vector2(MEM3.x*0.20f,MEM3.y*0.40f);
         Col1Section4a=new Vector2(MEM3.x*0.00f,MEM3.y*0.39f);
         Col1Section4b=new Vector2(MEM3.x*0.20f,MEM3.y*0.20f);
         Col1Section5a=new Vector2(MEM3.x*0.00f,MEM3.y*0.19f);
         Col1Section5b=new Vector2(MEM3.x*0.20f,MEM3.y*0.00f);
         */
     }        /*
     
             Original (Touch)
     
             x*0.0f Left
             x*1.0f Right
             y*0.0f Bottom
             y*1.0f Top
             
             Next (Screen.width*0.0f,Screen.height*0.0f)
             
             x*0.0f Left
             x*1.0f Right
             y*1.0f Bottom
             y*0.0f Top
             */        
 
     void OnGUI(){
         
         GUI.DrawTexture(JoystickRect,JoystickImages[1],ScaleMode.StretchToFill,true,Screen.width/Screen.height);
     }
     Rect JoystickRect=new Rect(Screen.width*0.00f,Screen.height*0.00f,150,150);
     //Rect JoystickRect=new Rect(Screen.width*0.05f,Screen.height*0.7f,150,150);
 
     void Update () {
         foreach (Touch touch in Input.touches){
         if (touch.phase== TouchPhase.Moved||touch.phase==TouchPhase.Began){
                 float Deconstructor1=touch.position.x;
                 float Deconstructor2=touch.position.y;
                 for (int i=0;i<5;++i){
                     if (Deconstructor1>Sectionsa[i].x&&Deconstructor1<Sectionsb[i].x&&Deconstructor2>Sectionsa[i].y&&Deconstructor2<Sectionsb[i].y){
                         JOYstandard=i;}else{}
             }}}}}
 

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jamora · Aug 03, 2013 at 01:06 PM

Unity has two sets of coordinates; the GUI coordinates and the Screen (or pixel) coordinates. GUI coordinates have their (0,0) in the top left corner, while Screen coordinates have their (0,0) in the bottom left.

I think only the GUI uses the GUI coordinates and all others use the Screen coordinates. You have to translate between these two. The translation can be done from from Screen ot GUI coordinates by swapping the y: (Screen.height-touch.position.y)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JMC17 · Aug 03, 2013 at 06:48 PM 0
Share

Thanks! That's it. I read something similar in another post and implemented it. Everything is working fine now!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Question about positioning GUI. 1 Answer

Is there a way to lock GUI buttons to a screen position insted of a pixel defined position so it will show up no matter the resolution of the screen? 0 Answers

GUITexture scaling with aspect ratio? 1 Answer

STRANGE error with screen.height/screen.width 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges