Advertisement
mysterious_3D

ThirPersonPlayerAnimation.js

Oct 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2. var runSpeedScale = 1.0;
  3. var walkSpeedScale = 1.0;
  4.  
  5. function Start ()
  6. {
  7.     // By default loop all animations
  8.     GetComponent.<Animation>().wrapMode = WrapMode.Loop;
  9.  
  10.     GetComponent.<Animation>()["run"].layer = -1;
  11.     GetComponent.<Animation>()["walk"].layer = -1;
  12.     GetComponent.<Animation>()["idle"].layer = -2;
  13.     GetComponent.<Animation>().SyncLayer(-1);
  14.  
  15.     GetComponent.<Animation>()["ledgefall"].layer = 9; 
  16.     GetComponent.<Animation>()["ledgefall"].wrapMode = WrapMode.Loop;
  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.Once;
  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.Loop;
  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.     {
  56.          GetComponent.<Animation>().CrossFade ("run");
  57.         // We fade out jumpland quick otherwise we get sliding feet
  58.         GetComponent.<Animation>().Blend("jumpland", 0);
  59.     }
  60.     // Fade in walk
  61.     else if (currentSpeed > 0.1)
  62.     {
  63.          GetComponent.<Animation>().CrossFade ("walk");
  64.         // We fade out jumpland realy quick otherwise we get sliding feet
  65.         GetComponent.<Animation>().Blend("jumpland", 0);
  66.     }
  67.     // Fade out walk and run
  68.     else
  69.     {
  70.         GetComponent.<Animation>().Blend("walk", 0.0, 0.3);
  71.         GetComponent.<Animation>().Blend("run", 0.0, 0.3);
  72.         GetComponent.<Animation>().Blend("idle", 0.0, 0.3);
  73.     }
  74.  
  75.    
  76.     GetComponent.<Animation>()["run"].normalizedSpeed = runSpeedScale;
  77.     GetComponent.<Animation>()["walk"].normalizedSpeed = walkSpeedScale;
  78.    
  79.     if (playerController.IsJumping ())
  80.     {
  81.         if (playerController.IsControlledDescent())
  82.         {
  83.             GetComponent.<Animation>().CrossFade ("jetpackjump", 0.2);
  84.         }
  85.         else if (playerController.HasJumpReachedApex ())
  86.         {
  87.             GetComponent.<Animation>().CrossFade ("jumpfall", 0.2);
  88.         }
  89.         else
  90.         {
  91.             GetComponent.<Animation>().Blend ("jump", 0.2);
  92.         }
  93.     }
  94.     // We fell down somewhere
  95.     else if (!playerController.IsGroundedWithTimeout())
  96.     {
  97.         GetComponent.<Animation>().CrossFade ("ledgefall", 0.2);
  98.     }
  99.     // We are not falling down anymore
  100.     else
  101.     {
  102.         GetComponent.<Animation>().Blend ("ledgefall", 0.0, 0.2);
  103.     }
  104. }
  105.  
  106. function DidLand () {
  107.     GetComponent.<Animation>().Play("jumpland");
  108. }
  109.  
  110. function DidButtStomp () {
  111.     GetComponent.<Animation>().CrossFade("buttstomp", 0.1);
  112.     GetComponent.<Animation>().CrossFadeQueued("jumpland", 0.2);
  113. }
  114.  
  115. function Slam () {
  116.     GetComponent.<Animation>().CrossFade("buttstomp", 0.2);
  117.     var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
  118.     while(!playerController.IsGrounded())
  119.     {
  120.         yield; 
  121.     }
  122.     GetComponent.<Animation>().Blend("buttstomp", 0, 0);
  123. }
  124.  
  125.  
  126. function DidWallJump ()
  127. {
  128.     // Wall jump animation is played without fade.
  129.     // We are turning the character controller 180 degrees around when doing a wall jump so the animation accounts for that.
  130.     // But we really have to make sure that the animation is in full control so
  131.     // that we don't do weird blends between 180 degree apart rotations
  132.     GetComponent.<Animation>().Play ("walljump");
  133. }
  134.  
  135. @script AddComponentMenu ("Third Person Player/Third Person Player Animation")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement