- Home /
Rotating GUI issues, issues with X,Y cords.
Hey, Im having some issues with rotating GUI.
I have a radar, and above that is a green sweeper thing that should rotate directly over the radar's GUI, Ive tried a few methods so far, and this has been the closest to working.
This main problem is that it finds the pivot of the GUI using X,Y as-well - this causes the thing to spin round in totally off ways at some resolutions, how can i correct my code to make it use a pivot that only relates to the texture?
public var testTexture: Texture2D = null;
var centerObject : Transform; var mapScale = 0.3; var mapSizePercent = 15;
enum sweepLocationValues {topLeft, topCenter, topRight, middleLeft, middleCenter, middleRight, bottomLeft, bottomCenter, bottomRight, custom} var sweepLocation : sweepLocationValues = sweepLocationValues.bottomLeft;
private var mapWidth : float;
private var mapHeight : float;
private var mapCenter : Vector2;
var mapCenterCustom : Vector2;
function Start () {
setMapLocation();
}
function OnGUI(){
//Here comes the tachoneedle rotation
var matrixBackup:Matrix4x4 = GUI.matrix;
var thisAngle:float = Time.frameCount * 2;
var pos:Vector2 = Vector2(50,830); // rotatepoint in texture plus x/y coordinates. our needle is at 16/16. Texture is 128/128. Makes middle 64 plus 16 = 80
GUIUtility.RotateAroundPivot(thisAngle, pos);
bX=centerObject.transform.position.x * mapScale;
bY=centerObject.transform.position.z * mapScale;
GUI.DrawTexture(Rect(mapCenter.x - mapWidth/2,mapCenter.y-mapHeight/2,mapWidth,mapHeight),testTexture);
GUI.matrix = matrixBackup;
}
function setMapLocation () {
mapWidth = Screen.width*mapSizePercent/100.0;
mapHeight = mapWidth;
//sets mapCenter based on enum selection
if(sweepLocation == sweepLocationValues.topLeft){
mapCenter = Vector2(mapWidth/2, mapHeight/2);
} else if(sweepLocation == sweepLocationValues.topCenter){
mapCenter = Vector2(Screen.width/2, mapHeight/2);
} else if(sweepLocation == sweepLocationValues.topRight){
mapCenter = Vector2(Screen.width-mapWidth/2, mapHeight/2);
} else if(sweepLocation == sweepLocationValues.middleLeft){
mapCenter = Vector2(mapWidth/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.middleCenter){
mapCenter = Vector2(Screen.width/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.middleRight){
mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.bottomLeft){
mapCenter = Vector2(mapWidth/2, Screen.height - mapHeight/2);
} else if(sweepLocation == sweepLocationValues.bottomCenter){
mapCenter = Vector2(Screen.width/2, Screen.height - mapHeight/2);
} else if(sweepLocation == sweepLocationValues.bottomRight){
mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height - mapHeight/2);
} else if(sweepLocation == sweepLocationValues.custom){
mapCenter = mapCenterCustom;
}
}
This is the flawed function...i think.
function OnGUI(){
//Here comes the tachoneedle rotation
var matrixBackup:Matrix4x4 = GUI.matrix;
var thisAngle:float = Time.frameCount * 2;
var pos:Vector2 = Vector2(50,830); // rotatepoint in texture plus x/y coordinates. our needle is at 16/16. Texture is 128/128. Makes middle 64 plus 16 = 80
GUIUtility.RotateAroundPivot(thisAngle, pos);
bX=centerObject.transform.position.x * mapScale;
bY=centerObject.transform.position.z * mapScale;
GUI.DrawTexture(Rect(mapCenter.x - mapWidth/2,mapCenter.y-mapHeight/2,mapWidth,mapHeight),testTexture);
GUI.matrix = matrixBackup;
}
What i would like todo, is have it take its coordinates like my radar script does, but then rotate around its own private axis - if that makes sense.
But i cant seem to get it right here,
var pos:Vector2 = Vector2(50,830);
I would really appreciate any help you could give me with this, its driving me insane!
Thankyou,
//" - Graeme."
Answer by ProgrammerArt · May 23, 2011 at 08:04 PM
Hi Graeme, I think I understand what's going on...
Your function 'setMapLocation' actually calculates the screen-space pivot point ('mapCenter') for the rotating gui element.
So 'mapCenter' is actually the position you want to pass to GUIUtility.RotateAroundPivot. If you did that, then the parameters you're currently passing to 'GUI.DrawTexture' would make the texture rotate around its centre.
But you want to rotate the texture around a point other than its centre. So you just have to shift the coords passed 'GUI.DrawTexture', to offset them by (textureCentre-texturePivot).
If you also want to scale the rotating texture, you would scale that offset as well as the dimensions passed to 'GUI.DrawTexture'.
You might want to call 'setMapLocation' every frame instead of just on startup, so that the gui positions & sizes update correctly if the window is resized or the resolution is changed.
Here's a modified version of the script that does what I think you want it to do. Let me know if I've misunderstood or if you have any questions :)
public var testTexture: Texture2D = null;
var centerObject : Transform;
enum sweepLocationValues {topLeft, topCentre, topRight, middleLeft, middleCentre, middleRight, bottomLeft, bottomCentre, bottomRight, custom}
var sweepLocation : sweepLocationValues = sweepLocationValues.middleCentre;
var screenSpacePivotCustom : Vector2 = Vector2(0,0);
// size of the radar background as a percentage of the screen width
private var backgroundSizePercent = 20;
// size of the radar overlay relative to the size of the radar background
private var overlaySizeFactor = 0.4;
// dimensions of the radar background
private var backgroundDimensions : Vector2;
// dimensions of the rotating radar overlay
private var overlayDimensions : Vector2;
// offset from the top-left of the overlay texture to the pivot of its needle
private var overlayLocalPivotOffsetFromTopLeft = Vector2(1/8.0, 1/8.0);// eg. needle pivot is at (16,16) on a 128x128 texture
// screen-space pivot for the rotating overlay
private var screenSpacePivot : Vector2;
function Start () {
UpdateDimensionsAndCoords();
}
function OnGUI(){
// Update the dimensions and pivot coords every frame, to account for changes in window size while running
UpdateDimensionsAndCoords();
//Here comes the tachoneedle rotation
var matrixBackup:Matrix4x4 = GUI.matrix;
var thisAngle:float = Time.frameCount * 2;
var pivot:Vector2 = screenSpacePivot;
GUIUtility.RotateAroundPivot(thisAngle, screenSpacePivot);
var needleOffsetFromTopLeft = Vector2(overlayDimensions.x * overlayLocalPivotOffsetFromTopLeft.x,
overlayDimensions.y * overlayLocalPivotOffsetFromTopLeft.y);
GUI.DrawTexture(Rect(screenSpacePivot.x-needleOffsetFromTopLeft.x,screenSpacePivot.y-needleOffsetFromTopLeft.y,overlayDimensions.x,overlayDimensions.y),testTexture);
GUI.matrix = matrixBackup;
}
// Update the gui dimensions and the screen-space pivot for the rotating radar overlay
function UpdateDimensionsAndCoords() {
backgroundDimensions.x = Screen.width*backgroundSizePercent/100.0;
backgroundDimensions.y = backgroundDimensions.x;
overlayDimensions = backgroundDimensions * overlaySizeFactor;
//sets screenSpacePivot based on enum selection
if(sweepLocation == sweepLocationValues.topLeft){
screenSpacePivot = Vector2(backgroundDimensions.x/2, backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.topCentre){
screenSpacePivot = Vector2(Screen.width/2, backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.topRight){
screenSpacePivot = Vector2(Screen.width-backgroundDimensions.x/2, backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.middleLeft){
screenSpacePivot = Vector2(backgroundDimensions.x/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.middleCentre){
screenSpacePivot = Vector2(Screen.width/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.middleRight){
screenSpacePivot = Vector2(Screen.width-backgroundDimensions.x/2, Screen.height/2);
} else if(sweepLocation == sweepLocationValues.bottomLeft){
screenSpacePivot = Vector2(backgroundDimensions.x/2, Screen.height - backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.bottomCentre){
screenSpacePivot = Vector2(Screen.width/2, Screen.height - backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.bottomRight){
screenSpacePivot = Vector2(Screen.width-backgroundDimensions.x/2, Screen.height - backgroundDimensions.y/2);
} else if(sweepLocation == sweepLocationValues.custom){
screenSpacePivot = screenSpacePivotCustom;
}
}