Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. private var previousXValue : float = 0;
  2. private var previousZValue : float = 0;
  3. private var moving : boolean = false;
  4. private var isIdle : boolean = true;
  5.  
  6. var walkAnimation : AnimationClip;
  7. var idleAnimation : AnimationClip;
  8. var idleBreak1Animation : AnimationClip;
  9. var idleBreak2Animation : AnimationClip;
  10.  
  11. walkAnimation.wrapMode=WrapMode.Loop;
  12. idleAnimation.wrapMode=WrapMode.Loop;
  13. idleBreak1Animation.wrapMode=WrapMode.Once;
  14. idleBreak2Animation.wrapMode=WrapMode.Once;
  15.  
  16. private var breakIdleAfterSeconds : int;
  17.  
  18. private var randomAnimation : int;
  19. private var idleTime : float = 0.0;
  20.  
  21. function Awake () {
  22. // At waking up we pick a random time to start our first idle animation
  23. pickRandomIdleTime();
  24. }
  25.  
  26. function Update(){
  27. //if we are idle just increment the idle time , else reset it
  28. idleTime=(isIdle) ? (idleTime+Time.deltaTime) : 0.0;
  29.  
  30. if (idleTime >= breakIdleAfterSeconds){
  31. // pick a random idle break animation
  32. var idleBreakAnimations = [idleBreak1Animation, idleBreak2Animation];
  33. randomAnimation= Mathf.RoundToInt(Random.Range(0, idleBreakAnimations.length));
  34.  
  35. animation.CrossFadeQueued(idleBreakAnimations[randomAnimation].name, 0.3, QueueMode.PlayNow);
  36. animation.CrossFadeQueued(idleAnimation.name, 0.3, QueueMode.CompleteOthers);
  37.  
  38. // get a new random idle break time to keep things dynamic :)
  39. idleTime=0.0;
  40. pickRandomIdleTime();
  41. }
  42.  
  43. // Check if we are actually roaming around , if so trigger the walking animation else, idle
  44. if (round(transform.position.x)!=previousXValue || round(transform.position.z)!=previousZValue){
  45. previousXValue=round(transform.position.x);
  46. previousZValue=round(transform.position.z);
  47. if (!moving){ SetMoving(true); }
  48. }else{
  49. if (moving){ SetMoving(false); }
  50. }
  51. }
  52.  
  53. function SetMoving(val){
  54. if (val){
  55. moving=true;
  56. isIdle=false;
  57. Debug.Log ( "we are moving and should play "+ walkAnimation.name );
  58. transform.animation.CrossFade(walkAnimation.name);
  59. }else{
  60. moving=false;
  61. isIdle=true;
  62. Debug.Log ( "we stopped moving" );
  63. transform.animation.CrossFade(idleAnimation.name);
  64. }
  65. }
  66.  
  67. //Something that we can fuzzely compare, in stead of 1.1231453252 we compare to 1.123
  68. function round(val){
  69. return float.Parse(val.ToString("f3"));
  70. }
  71.  
  72. function pickRandomIdleTime(){
  73. breakIdleAfterSeconds=Random.Range(15, 45);
  74. Debug.Log ( "I will break idle after " + breakIdleAfterSeconds + " seconds !");
  75. }
Add Comment
Please, Sign In to add comment