Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private var previousXValue : float = 0;
- private var previousZValue : float = 0;
- private var moving : boolean = false;
- private var isIdle : boolean = true;
- var walkAnimation : AnimationClip;
- var idleAnimation : AnimationClip;
- var idleBreak1Animation : AnimationClip;
- var idleBreak2Animation : AnimationClip;
- walkAnimation.wrapMode=WrapMode.Loop;
- idleAnimation.wrapMode=WrapMode.Loop;
- idleBreak1Animation.wrapMode=WrapMode.Once;
- idleBreak2Animation.wrapMode=WrapMode.Once;
- private var breakIdleAfterSeconds : int;
- private var randomAnimation : int;
- private var idleTime : float = 0.0;
- function Awake () {
- // At waking up we pick a random time to start our first idle animation
- pickRandomIdleTime();
- }
- function Update(){
- //if we are idle just increment the idle time , else reset it
- idleTime=(isIdle) ? (idleTime+Time.deltaTime) : 0.0;
- if (idleTime >= breakIdleAfterSeconds){
- // pick a random idle break animation
- var idleBreakAnimations = [idleBreak1Animation, idleBreak2Animation];
- randomAnimation= Mathf.RoundToInt(Random.Range(0, idleBreakAnimations.length));
- animation.CrossFadeQueued(idleBreakAnimations[randomAnimation].name, 0.3, QueueMode.PlayNow);
- animation.CrossFadeQueued(idleAnimation.name, 0.3, QueueMode.CompleteOthers);
- // get a new random idle break time to keep things dynamic :)
- idleTime=0.0;
- pickRandomIdleTime();
- }
- // Check if we are actually roaming around , if so trigger the walking animation else, idle
- if (round(transform.position.x)!=previousXValue || round(transform.position.z)!=previousZValue){
- previousXValue=round(transform.position.x);
- previousZValue=round(transform.position.z);
- if (!moving){ SetMoving(true); }
- }else{
- if (moving){ SetMoving(false); }
- }
- }
- function SetMoving(val){
- if (val){
- moving=true;
- isIdle=false;
- Debug.Log ( "we are moving and should play "+ walkAnimation.name );
- transform.animation.CrossFade(walkAnimation.name);
- }else{
- moving=false;
- isIdle=true;
- Debug.Log ( "we stopped moving" );
- transform.animation.CrossFade(idleAnimation.name);
- }
- }
- //Something that we can fuzzely compare, in stead of 1.1231453252 we compare to 1.123
- function round(val){
- return float.Parse(val.ToString("f3"));
- }
- function pickRandomIdleTime(){
- breakIdleAfterSeconds=Random.Range(15, 45);
- Debug.Log ( "I will break idle after " + breakIdleAfterSeconds + " seconds !");
- }
Add Comment
Please, Sign In to add comment