- Home /
Trying to code a simple animation, please help.
I made an animation it just changes between two different materials. I play the animation each time a prefab is called using getcomponent on the prefab, that seems to be working fine. My problem is I am not very fluent with the animator, and it plays all my animations right in the beginning of the scene. I try using animation.stop and animator.stopplayback I cant figure out how control the animator. I know its probably some check box, please forgive me if the answer is simple I am new! :-/
Can you please share the code? Can't do much without the code. Also give more details about what you're trying to accomplish.
Yes I can in the morning, but I am trying to accomplish a warning animation that an obstacle it's approaching in an infinite runner .
heres some code the animation auto plays in the beginning of scene so I try to stop it?
// From Prefab
public class DestroyAtX : $$anonymous$$onoBehaviour {
public float stopanim;
public float kill;
private Animator stoploganim;
public GameObject log;
// Use this for initialization
void Start () {
log = GameObject.Find("warn");
stoploganim = log.GetComponent<Animator>();
stoploganim.Play("GO");
}
// Update is called once per frame
void Update () {
if(transform.position.x < stopanim){
stoploganim.SetTrigger("STOP");
}
if(transform.position.x < kill){
DestroyObject(this.gameObject);
}
}
}
// From Plane with animation
public class WarnAnimTest : $$anonymous$$onoBehaviour {
private Animator warnstop;
// Use this for initialization
void Start () {
warnstop = GetComponent<Animator>();
warnstop.StopPlayback();
}
// Update is called once per frame
void Update () {
}
}
Answer by abi-kr01 · Nov 26, 2013 at 12:56 PM
Well if its only about two material rendering then u can use my code
what it basically does is it provide sprite animation, which uses images to play
you can try it it may work for your project
#pragma strict
class SpriteAnimJS extends MonoBehaviour
{
var frameA : Texture2D; //The first frame of the animation
var frameB : Texture2D; //The second frame of the animation
private var currentId : int = 0; //The ID of the current frame
private var canAnimate : boolean = false; //Animation enabled/disabled
//Called when the object is enabled
function OnEnable ()
{
//Enable the animation
canAnimate = true;
}
//Called when the object is disabled
function OnDisable()
{
//Stop the animation coroutine, and disable the animation
StopCoroutine("Animate");
canAnimate = false;
}
//Called on every frame
function Update()
{
//If the animation is enabled
if (canAnimate)
{
//Start the animation coroutine
StartCoroutine(Animate());
}
}
//The animation coroutine
function Animate()
{
//Disable the calling of additional coroutines
canAnimate = false;
//Wait for 0.1 seconds
yield WaitForSeconds(0.1f);
//If the current animation frame is 0
if (currentId == 0)
{
//Go to animation frame 1
this.renderer.material.mainTexture = frameB;
currentId = 1;
}
//If the current animation frame is 1
else
{
//Go to animation frame 0
this.renderer.material.mainTexture = frameA;
currentId = 0;
}
//Enable the calling of a new coroutine
canAnimate = true;
}
}
Cool, I'll check it out. Originally I was going that route and for some reason tried the animator route. The animator is a pain to control via code though. I simply want " if this happens ( either s function is invoked or a trigger is hit) then this animation will play."