- Home /
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);
}
}
}
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.
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
Your answer
Follow this Question
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