Advertisement
mysterious_3D

ThirdPersonPlayerAnimation.js (working)

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