Hey everyone! I'm working on an element allignment system for my own GUISystem. Something simple, but good for now. Everything works fine except for the ScreenToWorldPoint function, which don't give the results that I was expecting. I don't think there's something wrong with my code. Here it is:
public static Vector3 allignElement (Transform element, Allignment allignment) {
Camera mainCamera = Camera.mainCamera;
float distance = mainCamera.nearClipPlane;
Vector3 GUIZoneElementScale = element.localScale;
if(allignment == Allignment.BottomCenter) {
Vector3 inScreenPos = new Vector3(Screen.width/2, 0, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.BottomLeft) {
Vector3 inScreenPos = new Vector3(0, 0, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.BottomRight) {
Vector3 inScreenPos = new Vector3(Screen.width, 0, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.MiddleCenter) {
Vector3 inScreenPos = new Vector3(Screen.width/2, Screen.height/2, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.MiddleLeft) {
Vector3 inScreenPos = new Vector3(0, Screen.height/2, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.MiddleRight) {
Vector3 inScreenPos = new Vector3(Screen.width, Screen.height/2, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if (allignment == Allignment.None) {
return element.transform.position;
} else if(allignment == Allignment.UpperCenter) {
Vector3 inScreenPos = new Vector3(Screen.width/2, Screen.height, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if(allignment == Allignment.UpperLeft) {
Vector3 inScreenPos = new Vector3(0, Screen.height, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
} else if(allignment == Allignment.UpperRight) {
Vector3 inScreenPos = new Vector3(Screen.width, Screen.height, distance);
return mainCamera.ScreenToWorldPoint( inScreenPos );
}
return Vector3.zero;
}
The problem is when I want an object to be in one of the corners. For instance, UpperLeft gives me this:
![alt text][1]
[1]: /storage/temp/1758-test.jpg
Any idea of why this can be happening?
Thanks from now!
↧