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.
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).