- Home /
"Blinking Eyes" Texture Animations
I am making a game with toon-shaded graphics. The main character object has multiple textures and one of those is his eyes. I was trying to figure out how can I "animate" them though code? I was thinking that maybe I could make a script that has a timer and it would quickly swap the eye texture with one that looks like a blinking animation, that or use a .gif.
I also have other textures that gives his eyes expressions, but my only program is that I don't know how to code a texture swap/animation, especially for things like cut scenes that have separate animations that the in-game animations.
Also, the character model was made in blender, so if anyone could tell me how to change the texture via the blender Action Editor/IPO Curve Editor, that would be helpful.
How did your game came along? do you have a tumblr? or video?
@sirdavid23: This is not a forum to chit-chat. Even when you have a question to a post from someone else, use a comment and not an answer. This is a Q&A site and ment as knowledge database.
I've converted your answer into a comment.
Btw. This question is almost 2 years old...
Answer by Adam Rademacher · Oct 03, 2010 at 10:26 PM
If I understand your question right, you want to swap out textures on a timer to make the character 'blink'. Try something like this (javascript) -- Assuming that your eyes are on a different material than the rest of your character.
var normalEyes : Texture2D; //normal (open) eye texture var blinkyEyes : Texture2D; //blinking eye texture var eyeMaterial : Material; //eye material on player var eyeTimer : float; //time to keep eyes open var blinkTimer : float; //time to keep eyes closed var blinking : bool; //is the character currently blinking? private var currTime : float;//current timer var randomFactor : float; //a random factor to make it a little more interesting
function Start() { currTime = eyeTimer + Random.value * randomFactor; //establish current timer as open eyes eyeMaterial.mainTexture = normalEyes; //set eye texture to open blinking = false; //we are not blinking }
function Update() { if(currTime > 0.0f) //if the timer is greater than 0 { currTime -= Time.deltaTime; //subtract time } else //if the timer is up { if(blinking) //if the character is blinking { currTime = eyeTimer + (Random.value randomFactor); //reset the timer to the open time + a random value eyeMaterial.mainTexture = normalEyes; // set the texture to open eyes blinking = false; //we are no longer blinking } else //if the character is not blinking { currTime = blinkTimer + (Random.value randomFactor); //reset the timer to blinking time + a random value eyeMaterial.mainTexture = blinkyEyes; //set the texture to blinky eyes blinking = true; //we are now blinking } } }
You may find you want a different random factor for blinking and not blinking, but that should be simple enough to do (or you may find you don't want a random factor at all).
Thanks for the quick response! This is exactly what i was looking for!
For some reason, the textures won't swap anymore (i just started a new project) where should this script be placed?
It should go wherever you want it to. You'll have to re-assign the textures and material as well as reset the timers in the inspector.
Your answer
Follow this Question
Related Questions
Custom Pivot Points in Sprite Editor 1 Answer
Unity4.5 AnimationCurve - Is the property "_MainTex.rotation" valid ? 0 Answers
Animating Texture? 2 Answers
Pixel Color Issue 1 Answer
Set the procedural color texture script to a set pattern? 1 Answer