Advertisement
Guest User

CharacterAnimation.js

a guest
Nov 7th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.18 KB | None | 0 0
  1. //NSdesignGames @ 2012
  2. //FPS Kit | Version 2.0 + Multiplayer
  3.  
  4. //Use this script to controll thrird person animation
  5.  
  6. #pragma strict
  7.  
  8. import System.Collections.Generic;
  9.  
  10. //@script ExecuteInEditMode
  11.  
  12. //Use this gameObject to send messages to AnimationSync.cs
  13. var animationSyncHelper : GameObject;
  14. var animationForHands : GameObject;
  15. //This gameObject.name will tell AnimationSync.cs which third person weapon is active now
  16. var activeWeapon : GameObject;
  17. enum Action{Stand, Crouch, Prone}
  18. var action : Action;
  19.  
  20. class animations{
  21.     //Edit pose
  22.     //var poseAnimation : AnimationClip;
  23.     //Idle animations
  24.     var jumpPose : AnimationClip;
  25.     var stayIdle : AnimationClip;
  26.     var crouchIdle : AnimationClip;
  27.     var proneIdle : AnimationClip;
  28.     //Walk Animations
  29.     var walkFront : AnimationClip;
  30.     var walkBack : AnimationClip;
  31.     var walkLeft : AnimationClip;
  32.     var walkRight : AnimationClip;
  33.     var walkAnimationsSpeed : float = 1;
  34.     //Run animations
  35.     var runFront : AnimationClip;
  36.     var runAnimationsSpeed : float = 1;
  37.     //Crouch animations
  38.     var crouchFront : AnimationClip;
  39.     var crouchLeft : AnimationClip;
  40.     var crouchRight : AnimationClip;
  41.     var crouchBack : AnimationClip;
  42.     var crouchAnimationsSpeed : float = 1;
  43.     //Prone Animations
  44.     var proneFront : AnimationClip;
  45.     var proneLeft : AnimationClip;
  46.     var proneRight : AnimationClip;
  47.     var proneBack : AnimationClip;
  48.     var proneAnimationsSpeed : float = 1;
  49.     //Weapon animations
  50.     var pistolIdle : AnimationClip;
  51.     var knifeIdle : AnimationClip;
  52.     var gunIdle : AnimationClip;
  53.     /*var fire : AnimationClip;
  54.     var reload : AnimationClip;*/
  55. }
  56.  
  57. var Animations : animations;
  58.  
  59. var twoHandedWeapons : List.<WeaponScript>;
  60. var pistols : List.<WeaponScript>;
  61. var knivesNades : List.<WeaponScript>;
  62.  
  63. private var fpsController : FPScontroller;
  64. private var weapManager : WeaponManager;
  65.  
  66. function Start () {
  67.     fpsController = GameObject.FindWithTag("Player").GetComponent.<FPScontroller>();
  68.     configureAnimations();
  69.     weapManager = GameObject.FindWithTag("WeaponManager").GetComponent.<WeaponManager>();
  70.     //Play pose in the Editor
  71.     /*if(poseAnimation){
  72.         var state : AnimationState = animation[pistolIdle.name];
  73.         state.enabled = true;
  74.         state.weight = 1;
  75.         state.normalizedTime = 0;
  76.    
  77.         animation.Sample();
  78.     }*/
  79.     if(weapManager){
  80.         ThirdPersonWeaponControl();
  81.     }
  82. }
  83.  
  84. function Update () {
  85.     activeWeapon.name = weapManager.SelectedWeapon.weaponName;
  86.     if(!fpsController.crouch && !fpsController.prone){
  87.         action = Action.Stand;
  88.     }
  89.     else if(fpsController.crouch && !fpsController.prone){
  90.         action = Action.Crouch;
  91.     }
  92.     else if(!fpsController.crouch && fpsController.prone){
  93.         action = Action.Prone;
  94.     }
  95.    
  96.     if(action == Action.Stand){
  97.         if(fpsController.grounded){
  98.             if(fpsController.Walking && !fpsController.Running){
  99.                 if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
  100.                     animation.CrossFade(Animations.walkFront.name, 0.2);
  101.                     //Send animation name (needed for multiplayer)
  102.                     animationSyncHelper.name = Animations.walkFront.name;
  103.                 }
  104.                 else if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.S)){
  105.                     animation.CrossFade(Animations.walkLeft.name, 0.2);
  106.                     //Send animation name (needed for multiplayer)
  107.                     animationSyncHelper.name = Animations.walkLeft.name;
  108.                 }
  109.                 else if( Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.S)){
  110.                     animation.CrossFade(Animations.walkRight.name, 0.2);
  111.                     //Send animation name (needed for multiplayer)
  112.                     animationSyncHelper.name = Animations.walkRight.name;
  113.                 }
  114.                 else if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){
  115.                     animation.CrossFade(Animations.walkBack.name, 0.2);
  116.                     //Send animation name (needed for multiplayer)
  117.                     animationSyncHelper.name = Animations.walkBack.name;
  118.                 }
  119.             }
  120.             else if(fpsController.Walking && fpsController.Running){
  121.                 if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
  122.                     animation.CrossFade(Animations.runFront.name, 0.2);
  123.                     //Send animation name (needed for multiplayer)
  124.                     animationSyncHelper.name = Animations.runFront.name;
  125.                 }
  126.             }
  127.             if(!fpsController.Walking){
  128.                 animation.CrossFade(Animations.stayIdle.name, 0.2);
  129.                 //Send animation name (needed for multiplayer)
  130.                 animationSyncHelper.name = Animations.stayIdle.name;
  131.             }
  132.         }else{
  133.                 animation.CrossFade(Animations.jumpPose.name, 0.2);
  134.                 //Send animation name (needed for multiplayer)
  135.                 animationSyncHelper.name = Animations.jumpPose.name;
  136.         }
  137.     }
  138.     if(action == Action.Crouch){
  139.         if(fpsController.Walking ){
  140.             if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
  141.                 animation.CrossFade(Animations.crouchFront.name, 0.2);
  142.                 //Send animation name (needed for multiplayer)
  143.                 animationSyncHelper.name = Animations.crouchFront.name;
  144.             }
  145.             else if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.S)){
  146.                 animation.CrossFade(Animations.crouchLeft.name, 0.2);
  147.                 //Send animation name (needed for multiplayer)
  148.                 animationSyncHelper.name = Animations.crouchLeft.name;
  149.             }
  150.             else if( Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.S)){
  151.                 animation.CrossFade(Animations.crouchRight.name, 0.2);
  152.                 //Send animation name (needed for multiplayer)
  153.                 animationSyncHelper.name = Animations.crouchRight.name;
  154.             }
  155.             else if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){
  156.                 animation.CrossFade(Animations.crouchBack.name, 0.2);
  157.                 //Send animation name (needed for multiplayer)
  158.                 animationSyncHelper.name = Animations.crouchBack.name;
  159.             }
  160.         }else{
  161.             animation.CrossFade(Animations.crouchIdle.name, 0.2);
  162.             //Send animation name (needed for multiplayer)
  163.             animationSyncHelper.name = Animations.crouchIdle.name;
  164.         }
  165.     }
  166.     if(action == Action.Prone){
  167.         if(fpsController.Walking ){
  168.             if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
  169.                 animation.CrossFade(Animations.proneFront.name, 0.2);
  170.                 //Send animation name (needed for multiplayer)
  171.                 animationSyncHelper.name = Animations.proneFront.name;
  172.             }
  173.             else if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.S)){
  174.                 animation.CrossFade(Animations.proneLeft.name, 0.2);
  175.                 //Send animation name (needed for multiplayer)
  176.                 animationSyncHelper.name = Animations.proneLeft.name;
  177.             }
  178.             else if( Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.S)){
  179.                 animation.CrossFade(Animations.proneRight.name, 0.2);
  180.                 //Send animation name (needed for multiplayer)
  181.                 animationSyncHelper.name = Animations.proneRight.name;
  182.             }
  183.             else if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){
  184.                 animation.CrossFade(Animations.proneBack.name, 0.2);
  185.                 //Send animation name (needed for multiplayer)
  186.                 animationSyncHelper.name = Animations.proneBack.name;
  187.             }
  188.         }else{
  189.             animation.CrossFade(Animations.proneIdle.name, 0.2);
  190.             //Send animation name (needed for multiplayer)
  191.             animationSyncHelper.name = Animations.proneIdle.name;
  192.         }
  193.     }
  194.     ThirdPersonWeaponControl();
  195. }
  196.  
  197. function ThirdPersonWeaponControl(){
  198.     if(action != Action.Prone){
  199.         if(twoHandedWeapons.Contains(weapManager.SelectedWeapon)){
  200.             animationForHands.name = Animations.gunIdle.name;
  201.         }
  202.         else if(pistols.Contains(weapManager.SelectedWeapon)){
  203.             animationForHands.name = Animations.pistolIdle.name;
  204.         }
  205.         else if(knivesNades.Contains(weapManager.SelectedWeapon)){
  206.             animationForHands.name = Animations.knifeIdle.name;
  207.         }
  208.     }else{
  209.         animationForHands.name = "Null";
  210.     }
  211. }
  212.  
  213. function configureAnimations(){
  214.     //Set animations Wrap Mode and Speed
  215.     if(Animations.stayIdle){
  216.         animation[Animations.stayIdle.name].wrapMode = WrapMode.Loop;
  217.     }
  218.     if(Animations.crouchIdle){
  219.         animation[Animations.crouchIdle.name].wrapMode = WrapMode.Loop;
  220.     }
  221.     if(Animations.proneIdle){
  222.         animation[Animations.proneIdle.name].wrapMode = WrapMode.Loop;
  223.     }
  224.     if(Animations.walkFront){
  225.         animation[Animations.walkFront.name].wrapMode = WrapMode.Loop;
  226.         animation[Animations.walkFront.name].speed = Animations.walkAnimationsSpeed;
  227.     }
  228.     if(Animations.walkBack){
  229.         animation[Animations.walkBack.name].wrapMode = WrapMode.Loop;
  230.         animation[Animations.walkBack.name].speed = Animations.walkAnimationsSpeed;
  231.     }
  232.     if(Animations.walkLeft){
  233.         animation[Animations.walkLeft.name].wrapMode = WrapMode.Loop;
  234.         animation[Animations.walkLeft.name].speed = Animations.walkAnimationsSpeed;
  235.     }
  236.     if(Animations.walkRight){
  237.         animation[Animations.walkRight.name].wrapMode = WrapMode.Loop;
  238.         animation[Animations.walkRight.name].speed = Animations.walkAnimationsSpeed;
  239.     }
  240.     if(Animations.runFront){
  241.         animation[Animations.runFront.name].wrapMode = WrapMode.Loop;
  242.         animation[Animations.runFront.name].speed = Animations.runAnimationsSpeed;
  243.     }
  244.     if(Animations.crouchFront){
  245.         animation[Animations.crouchFront.name].wrapMode = WrapMode.Loop;
  246.         animation[Animations.crouchFront.name].speed = Animations.crouchAnimationsSpeed;
  247.     }
  248.     if(Animations.crouchLeft){
  249.         animation[Animations.crouchLeft.name].wrapMode = WrapMode.Loop;
  250.         animation[Animations.crouchLeft.name].speed = Animations.crouchAnimationsSpeed;
  251.     }
  252.     if(Animations.crouchRight){
  253.         animation[Animations.crouchRight.name].wrapMode = WrapMode.Loop;
  254.         animation[Animations.crouchRight.name].speed = Animations.crouchAnimationsSpeed;
  255.     }
  256.     if(Animations.crouchBack){
  257.         animation[Animations.crouchBack.name].wrapMode = WrapMode.Loop;
  258.         animation[Animations.crouchBack.name].speed = Animations.crouchAnimationsSpeed;
  259.     }
  260.     if(Animations.proneFront){
  261.         animation[Animations.proneFront.name].wrapMode = WrapMode.Loop;
  262.         animation[Animations.proneFront.name].speed = Animations.proneAnimationsSpeed;
  263.     }
  264.     if(Animations.proneLeft){
  265.         animation[Animations.proneLeft.name].wrapMode = WrapMode.Loop;
  266.         animation[Animations.proneLeft.name].speed = Animations.proneAnimationsSpeed;
  267.     }
  268.     if(Animations.proneRight){
  269.         animation[Animations.proneRight.name].wrapMode = WrapMode.Loop;
  270.         animation[Animations.proneRight.name].speed = Animations.proneAnimationsSpeed;
  271.     }
  272.     if(Animations.proneBack){
  273.         animation[Animations.proneBack.name].wrapMode = WrapMode.Loop;
  274.         animation[Animations.proneBack.name].speed = Animations.proneAnimationsSpeed;
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement