Advertisement
mysterious_3D

My code for character animation

Sep 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var runSpeedScale = 1.0;
  2. var walkSpeedScale = 1.0;
  3.  
  4. function Start ()
  5. {
  6.     // By default loop all animations
  7.     GetComponent.<Animation>().wrapMode = WrapMode.Loop;
  8.  
  9.     GetComponent.animation["run"].layer = -1;
  10.     GetComponent.animation["walk"].layer = 1;
  11.     GetComponent.Animation["idle"].layer = 0;
  12.     GetComponent.animation.SyncLayer(-1);
  13.  
  14.     GetComponent.animation["ledgefall"].layer = 9; 
  15.     GetComponent.animation["ledgefall"].wrapMode = WrapMode.Loop;
  16.  
  17.  
  18.     // The jump animation is clamped and overrides all others
  19.     GetComponent.animation["jump"].layer = 10;
  20.     GetComponent.animation["jump"].wrapMode = WrapMode.ClampForever;
  21.  
  22.     GetComponent.animation["jumpfall"].layer = 10; 
  23.     GetComponent.animation["jumpfall"].wrapMode = WrapMode.ClampForever;
  24.  
  25.     // This is the jet-pack controlled descent animation.
  26.     GetComponent.animation["jetpackjump"].layer = 10;  
  27.     GetComponent.animation["jetpackjump"].wrapMode = WrapMode.ClampForever;
  28.  
  29.     GetComponent.animation["jumpland"].layer = 10; 
  30.     GetComponent.animation["jumpland"].wrapMode = WrapMode.Once;
  31.  
  32.     GetComponent.animation["walljump"].layer = 11; 
  33.     GetComponent.animation["walljump"].wrapMode = WrapMode.Once;
  34.  
  35.     // we actually use this as a "got hit" animation
  36.     GetComponent.animation["buttstomp"].speed = 0.15;
  37.     GetComponent.animation["buttstomp"].layer = 20;
  38.     GetComponent.animation["buttstomp"].wrapMode = WrapMode.Once;  
  39.     var punch = GetComponent.animation("punch");
  40.     punch.wrapMode = WrapMode.Once;
  41.  
  42.     // We are in full control here - don't let any other animations play when we start
  43.     GetComponent.animation.Stop();
  44.     GetComponent.animation.Play("idle");
  45. }
  46.  
  47. function Update ()
  48. {
  49.     var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
  50.     var currentSpeed = playerController.GetSpeed();
  51.  
  52.     // Fade in run
  53.     if (currentSpeed > playerController.walkSpeed)
  54.     {
  55.         GetComponent.animation.CrossFade("run");
  56.         // We fade out jumpland quick otherwise we get sliding feet
  57.         GetComponent.animation.Blend("jumpland", 0);
  58.     }
  59.     // Fade in walk
  60.     else if (currentSpeed > 0.1)
  61.     {
  62.         GetComponent.animation.CrossFade("walk");
  63.         // We fade out jumpland realy quick otherwise we get sliding feet
  64.         GetComponent.animation.Blend("jumpland", 0);
  65.     }
  66.     // Fade out walk and run
  67.     else
  68.     {
  69.         GetComponent.animation.Blend("walk", 0.0, 0.3);
  70.         GetComponent.animation.Blend("run", 0.0, 0.3);
  71.         GetComponent.animation.Blend("run", 0.0, 0.3);
  72.     }
  73.    
  74.     GetComponent.animation("run").normalizedSpeed = runSpeedScale;
  75.     GetComponent.animation("walk").normalizedSpeed = walkSpeedScale;
  76.    
  77.     if (playerController.IsJumping ())
  78.     {
  79.         if (playerController.IsControlledDescent())
  80.         {
  81.             GetComponent.animation.CrossFade ("jetpackjump", 0.2);
  82.         }
  83.         else if (playerController.HasJumpReachedApex ())
  84.         {
  85.             GetComponent.animation.CrossFade ("jumpfall", 0.2);
  86.         }
  87.         else
  88.         {
  89.             GetComponent.animation.CrossFade ("jump", 0.2);
  90.         }
  91.     }
  92.     // We fell down somewhere
  93.     else if (!playerController.IsGroundedWithTimeout())
  94.     {
  95.         GetComponent.animation.CrossFade ("ledgefall", 0.2);
  96.     }
  97.     // We are not falling down anymore
  98.     else
  99.     {
  100.         GetComponent.animation.Blend ("ledgefall", 0.0, 0.2);
  101.     }
  102. }
  103.  
  104. function DidLand () {
  105.     GetComponent.animation.Play("jumpland");
  106. }
  107.  
  108. function DidButtStomp () {
  109.     GetComponent.animation.CrossFade("buttstomp", 0.1);
  110.     GetComponent.animation.CrossFadeQueued("jumpland", 0.2);
  111. }
  112.  
  113. function Slam () {
  114.     GetComponent.animation.CrossFade("buttstomp", 0.2);
  115.     var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
  116.     while(!playerController.IsGrounded())
  117.     {
  118.         yield; 
  119.     }
  120.     GetComponent.animation.Blend("buttstomp", 0, 0);
  121. }
  122.  
  123.  
  124. function DidWallJump ()
  125. {
  126.     // Wall jump animation is played without fade.
  127.     // We are turning the character controller 180 degrees around when doing a wall jump so the animation accounts for that.
  128.     // But we really have to make sure that the animation is in full control so
  129.     // that we don't do weird blends between 180 degree apart rotations
  130.     GetComponent.animation.Play ("walljump");
  131. }
  132.  
  133. @script AddComponentMenu ("Third Person Player/Third Person Player Animation")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement