• 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 Chimera3D · Nov 04, 2012 at 08:13 AM · rotationguitextureobjectangle

Rotating GUI Texture By Angle

So I used Ben Pitt's code from this post: http://answers.unity3d.com/questions/11022/how-to-rotate-gui-textures.html to get the rotation part working. Now what I need is for the texture to always point toward a game object in the scene, I'm not sure how to do this, I've tried a few things that didn't work. Any ideas?

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

2 Replies

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

Answer by Scribe · Nov 04, 2012 at 11:54 AM

Hopefully this works for you, unfortunately I can only really read C# and can't write it so easily so I've written this in unityscript, but maybe you can convert the additions I've made, or possibly unityscript is fine for you...

 var texture : Texture2D = null;
 var angle : float = 0;
 var size : Vector2 = new Vector2(128, 128);
 var pos : Vector2 = new Vector2(0, 0);
 var rect : Rect;
 var pivot : Vector2;
 
 var targetObj : GameObject;
 var screenPos : Vector2;
 var offset : Vector2;
 var Rads : float = 0;
 
 function Start() {
     UpdateSettings();
 }
 
 function Update() {
     screenPos = Camera.main.WorldToScreenPoint(targetObj.transform.position);
     screenPos.y = Screen.height-screenPos.y;
     offset = screenPos - pos;
     Rads = Mathf.Atan2(offset.y, offset.x)-(Mathf.PI/2);
     RadDegConvert(Rads);
 }
 
 function UpdateSettings() {
     pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
     rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
     pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
 }
 
 function OnGUI() {
     if (Application.isEditor) { UpdateSettings(); }
     var matrixBackup : Matrix4x4 = GUI.matrix;
     GUIUtility.RotateAroundPivot(angle, pivot);
     GUI.DrawTexture(rect, texture);
     GUI.matrix = matrixBackup;
 }
 
 function RadDegConvert(radians) {
     angle = radians*360/(Mathf.PI*2);
 }

hope that works for you,

Scribe

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 Chimera3D · Nov 05, 2012 at 04:52 AM 0
Share

Unityscript is perfect because that's what I converted the code to anyways.

avatar image
0

Answer by Chimera3D · Nov 05, 2012 at 05:08 AM

Your code worked perfectly except for one part... it was inverted. To fix that I added the lines `pos.y = Screen.height - pos.y`; and `screenPos.y = Screen.height - screenPos.y; before you calculate the offset. This is something I found out about very recently. Thanks!

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 Scribe · Nov 05, 2012 at 03:51 PM 0
Share

Glad you got it to work, and thanks for pointing that out! I'll know for next time :D

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Testing if object rotation differs from given rotation and rotating towards it? 1 Answer

Limit Object Angle 2 Answers

Rotating a Rotated GUI Texture 0 Answers

How to use On-Screen Buttons in unity 5 {URGENT} 1 Answer

Limit GUI Rotation? 2 Answers


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