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 Graeme P · May 03, 2011 at 03:26 PM · guirotateissueonguipivot

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."

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
1
Best Answer

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;
     }
 } 
Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

OnGui Function help 1 Answer

Draw movetool gizmo in OnDrawGizmos() 1 Answer

My OnGUI() Won't show the Button elements :( 0 Answers

Drawing a GUI.DrawTexture call above GUI.Button in seperate scripts 0 Answers

how to get Gui in OnGUI function to resize with the window? 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