- Home /
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{}
}}}}}
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)
Thanks! That's it. I read something similar in another post and implemented it. Everything is working fine now!
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Question about positioning GUI. 1 Answer