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 davidflynn2 · Aug 20, 2013 at 12:41 AM · c#texturerotateover time

Rotate Texture Over Time

I have the following script I am using for creating an Radar system. I have recently add code to draw a texture called texture. This has a declaration for it that allows it to rotate and its called angle. I need to know how to cause the angle to change to cause it to spin over time forever.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
 
     public Texture blip;
     public Texture blip2;
     public Texture radarBG;
     public Texture radarFG;
     public Transform centerObject;
     public float mapScale = 0.3f;
     public Vector2 mapCenter;
     public float screenWidth=1024.0f;
     public float screenHeight=768.0f;
     public int mapLocation = 3;
 
     public string tagFilter = "Enemy";
     public string tagFilter2 = "Planet";
     public float maxDist = 200;
     
     public Texture2D texture = null;
     public float angle = 0;
     public Vector2 size = new Vector2(128, 128);
     public Vector2 pos = new Vector2(0, 0);
     Rect rect;
     Vector2 pivot;
 
 
  void Start() 
     {
         UpdateSettings();
     }
     
     
     void UpdateSettings() 
     {
         rect = new Rect(new Rect(screenWidth - radarBG.width, screenHeight - radarBG.height, radarBG.width, radarBG.height));
         pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
    }
 
 
 
     public void OnGUI()
     {
         
 
       
         GUI.DrawTexture(new Rect(screenWidth - radarBG.width, screenHeight - radarBG.height, radarBG.width, radarBG.height), radarBG);
           UnityEngine.GUI.DrawTexture(new Rect(screenWidth - radarBG.width, screenHeight - radarBG.height, radarBG.width, radarBG.height), radarBG);
         
           mapCenter = new Vector2(screenWidth-(radarBG.width / 2),screenHeight-(radarBG.height / 2));
           
         if (Application.isEditor) { UpdateSettings(); }
         Matrix4x4 matrixBackup = GUI.matrix;
         GUIUtility.RotateAroundPivot(angle, pivot);
         GUI.DrawTexture(rect, texture);
         GUI.matrix = matrixBackup;
 
       DrawBlipsFor(tagFilter);
       DrawBlipsFor2(tagFilter2);
         
         
 
 
     }
  
     private void DrawBlipsFor(string tagName)
     {
       // Find all game objects with tag 
       GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
       // Iterate through them
       foreach (GameObject go in gos)
       {
         drawBlip(go, blip);
       }
     }
      private void DrawBlipsFor2(string tagName)
     {
       // Find all game objects with tag 
       GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
       // Iterate through them
       foreach (GameObject go in gos)
       {
         drawBlip(go, blip2);
       }
     }
  
     private void drawBlip(GameObject go, Texture aTexture)
     {
       Vector3 centerPos = centerObject.position;
       Vector3 extPos = go.transform.position;
  
       // first we need to get the distance of the enemy from the player
       float dist = Vector3.Distance(centerPos, extPos);
  
       float dx = centerPos.x - extPos.x; // how far to the side of the player is the enemy?
       float dz = centerPos.z - extPos.z; // how far in front or behind the player is the enemy?
  
       // what's the angle to turn to face the enemy - compensating for the player's turning?
       float deltay = Mathf.Atan2(dx, dz) * Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
  
       // just basic trigonometry to find the point x,y (enemy's location) given the angle deltay
       float bX = dist * Mathf.Cos(deltay * Mathf.Deg2Rad);
       float bY = dist * Mathf.Sin(deltay * Mathf.Deg2Rad);
  
       bX = bX * mapScale; // scales down the x-coordinate by half so that the plot stays within our radar
       bY = bY * mapScale; // scales down the y-coordinate by half so that the plot stays within our radar
  
       if (dist <= maxDist)
       {
         // this is the diameter of our largest radar circle
         UnityEngine.GUI.DrawTexture(new Rect(mapCenter.x + bX, mapCenter.y + bY, 10, 10), aTexture);
       }
     }
   }
 
Comment
Add comment · Show 1
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 OP_toss · Aug 20, 2013 at 01:04 AM 0
Share

Sorry this isn't an answer to your question exactly, but I highly recommend using a GUI system other than the built-in Unity GUI. It is both incredibly slow, and heavy on code. It also doesn't support a good deal of features.

There's many alternatives to choose from, some popular ones including NGui, EZGui, UIToolkit, and Scaleform.

I'd guess that's why no one's answering this, is because no one uses Unity GUI.

1 Reply

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

Answer by clunk47 · Aug 20, 2013 at 02:00 AM

Just use angle += (int), like the example in Unity Script Ref. Make sure your angle + is in Update or OnGUI so it is being fired continuously.

CLICK HERE for more info on RotateAroundPivot

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Images Rotation 1 Answer

Rotation and Translation problem 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