- Home /
Animating Texture?
I have an image that I am drawing to the screen using the "DrawTexture()" function. I want to animate the texture to set a different scale and position. I tried using "Lerp", but it's not letting me animate the position on both axes at the same time. Here's the code that I have:
var xPos : float = Screen.width / 2 - 40; //starting x position
var yPos : float = Screen.height / 2 - 30; //starting y position
function DrawWeaponTexture()
{
var originalColor = GUI.color;
xPos = Mathf.Lerp(xPos, Screen.width -100,Time.deltaTime); //animate x position of image
yPos = Mathf.Lerp(yPos, 20,Time.deltaTime); //animate y position of image
GUI.color = Color(1.0, 1.0, 1.0, .5);
GUI.DrawTexture(Rect(xPos, yPos,80,80), axeTexture);
GUI.color = originalColor;
}
Also, does anyone know of a site that can explain Lerp, Slerp, and all that stuff at a level that can be understood without taking an in-depth trigonometry course?
Answer by tomekkie2 · Jun 16, 2012 at 09:57 AM
When you put your DrawWeaponTexture() inside the OnGUI() function it will animate both axes, because I've just tested that and it does. The problem is how is the Unity supposed to know the Screen.width and height parameters? In order to do it in the controlled way - I would slightly modificate your script:
private var xPos: float;
private var yPos: float;
var axeTexture: Texture;
function Start () {
xPos = Screen.width / 2 - 40; //starting x position
yPos = Screen.height / 2 - 30; //starting y position
}
function DrawWeaponTexture()
{
//var originalColor = GUI.color;
xPos = Mathf.Lerp(xPos, Screen.width -100,Time.deltaTime); //animate x position of image
yPos = Mathf.Lerp(yPos, 20,Time.deltaTime); //animate y position of image
GUI.color = Color(1.0, 1.0, 1.0, .5);
GUI.DrawTexture(Rect(xPos, yPos,80,80), axeTexture);
// GUI.color = originalColor;
}
function OnGUI (){
DrawWeaponTexture();
}
Answer by whydoidoit · Jun 16, 2012 at 10:00 AM
function DrawWeaponTexture() {
if( Event.current.type != EventType.repaint) return;
Will help because OnGUI functions are called multiple times per frame and aren't very compatible with Lerp etc.