Search Unity

Need To Extend This Script - I Need Help

Discussion in 'Scripting' started by letsconnectcode, Jul 20, 2015.

  1. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    Hey Community,

    This is my first post here on the Forums, I am having trouble with extending this script.

    Right now there is two basic animations (Walk and Idle) which trigger either when the "W" key is down or when no key is pressed. I need to know how to extend this script so that I can add a Reload, Sprint, Shoot animations.

    I am looking for a answer with the script ready but a explanation on how you did it so I can learn from it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimationManager : MonoBehaviour {
    5.  
    6.     public bool isWalking = false;
    7.  
    8.     void Start() {
    9.    
    10.         animation.CrossFade("idle");
    11.    
    12.     }
    13.    
    14.     void Update() {
    15.    
    16.         if(!animation.IsPlaying("walk")) {
    17.        
    18.             isWalking = false;
    19.        
    20.         }
    21.            
    22.         if(Input.GetKeyDown(KeyCode.W)) {
    23.        
    24.             isWalking = true;
    25.        
    26.         }
    27.        
    28.         if(isWalking == true) {
    29.        
    30.              animation.CrossFade("walk");
    31.        
    32.         }
    33.         else {
    34.        
    35.             animation.CrossFade("idle");
    36.        
    37.         }
    38.        
    39.         if(Input.GetKeyUp(KeyCode.W)) {
    40.    
    41.             isWalking = false;
    42.             animation.CrossFade("idle");
    43.        
    44.         }
    45.        
    46.         if(isWalking == false) {
    47.        
    48.             animation.CrossFade("idle");
    49.        
    50.         }
    51.    
    52.     }
    53.    
    54. }
    Thank You,

    Mike.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    looks like this is from the legacy animation system (might want to include "legacy animation" in the thread title, title edit option is top right in "thread tools" :p ).

    Are you particularly attached to using this script / the legacy animation system or would you consider using the new mecanim/animator state system approach?
     
  3. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    Sorry my title wasn't clear enough. :(.

    I am looking to continue to use legacy animations as I am for comfortable and familiar with. Do you know how to extend this script?
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Wouldn't you add more bools for the extra States then add in the extra if statements defining the key combos you want & the animations you want to call similar to what you already have?
     
  5. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    I had tried that before and k kept on getting a error. I'm out now so I can't tell, I'll post a tried version of adding more a I,actions and maybe y'all can help out a bit with the error. I should be back in about 5 hours.

    Thanks you for replies guys.
     
  6. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    Alright, so I have tried adding more bools and extending the script... still no luck. Excuse my weak scripting here, i'm still a beginner.

    Can someone please help? I would truly appreciate that!
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    post the extended script you tried, it might just be a simple issue
     
  8. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    Please excuse any pointless mistakes, I don't have a lot of knowledge in scripting. I am more of a map designer and basic modeler.

    Code (CSharp):
    1.     1    using UnityEngine;
    2.     2    using System.Collections;
    3.     3    
    4.     4    public class AnimationManager : MonoBehaviour {
    5.     5    
    6.     6        public bool isWalking = false;
    7.     7        public bool isReoloading = false;
    8.     8        public bool isSprinting = false;
    9.     9    
    10.     10        void Start() {
    11.     11      
    12.     12            animation.CrossFade("idle");
    13.     13      
    14.     14        }
    15.     15      
    16.     16        void Update() {
    17.     17      
    18.     18            if(!animation.IsPlaying("walk")) {
    19.     19          
    20.     20                isWalking = false;
    21.     21          
    22.     22            }
    23.     23              
    24.     24            if(Input.GetKeyDown(KeyCode.W)) {
    25.     25          
    26.     26                isWalking = true;
    27.     27          
    28.     28            }
    29.     29          
    30.     30            if(isWalking == true) {
    31.     31          
    32.     32                 animation.CrossFade("walk");
    33.     33          
    34.     34            }
    35.     35            else {
    36.     36          
    37.     37                animation.CrossFade("idle");
    38.     38          
    39.     39            }
    40.     40          
    41.     41            if(Input.GetKeyUp(KeyCode.W)) {
    42.     42      
    43.     43                isWalking = false;
    44.     44                animation.CrossFade("idle");
    45.     45          
    46.     46            }
    47.     47          
    48.     48            if(isWalking == false) {
    49.     49          
    50.     50                animation.CrossFade("idle");
    51.     51          
    52.     52            }
    53.     53       if(Input.GetKeyUp(KeyCode.R)) {
    54.     54      
    55.     55                isWalking = false;
    56.     56                animation.CrossFade(“Reload”);
    57.     57          
    58.     58            }
    59.     59          
    60.     60            if(isWalking == false) {
    61.     61            if(isSprnting == false) {
    62.     62          
    63.     63                animation.CrossFade("idle");
    64.     64          
    65.     65            }
    66.     66      
    67.     67        }
    68.     68      
    69.     69    }
    Its midnight here and I kinda had a long day, so my brain isn't functioning properly.

    I don't want to sound rude, but do you think you could write the script so that it functions properly when a certain key is pressed (for four animations); sprinting, walking, idle, reload...

    Apologizes for my low skilled knowledge in scripting, and appreciate your help :).
     
  9. letsconnectcode

    letsconnectcode

    Joined:
    Oct 18, 2014
    Posts:
    6
    Anyone? Would really appreciate some help here :(
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I'm not up on animation (still trying to code mechanics first) but it looks like something is missing as Reload is showing as a different colour. Maybe it has no reference to Reload (or there's just something wrong with the way it pasted here)?
     
  11. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I think part of why you're having problems is because the original script you're working from is already very difficult to understand at best. I'd recommend first refactoring that code to separate the input processing logic from the animation assignment logic first, and then you'll have a better idea of how to proceed with adding new states. As it is, you'd be setting yourself up for disaster without an appropriate state machine, and every new "state" you add will just be another strand of spaghetti in the bowl.