Need To Extend This Script - I Need Help

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.

using UnityEngine;
using System.Collections;

public class AnimationManager : MonoBehaviour {

    public bool isWalking = false;

    void Start() {
   
        animation.CrossFade("idle");
   
    }
   
    void Update() {
   
        if(!animation.IsPlaying("walk")) {
       
            isWalking = false;
       
        }
           
        if(Input.GetKeyDown(KeyCode.W)) {
       
            isWalking = true;
       
        }
       
        if(isWalking == true) {
       
             animation.CrossFade("walk");
       
        }
        else {
       
            animation.CrossFade("idle");
       
        }
       
        if(Input.GetKeyUp(KeyCode.W)) {
   
            isWalking = false;
            animation.CrossFade("idle");
       
        }
       
        if(isWalking == false) {
       
            animation.CrossFade("idle");
       
        }
   
    }
   
}

Thank You,

Mike.

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” :stuck_out_tongue: ).

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

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?

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?

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.

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!

post the extended script you tried, it might just be a simple issue

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.

    1    using UnityEngine;
    2    using System.Collections;
    3     
    4    public class AnimationManager : MonoBehaviour {
    5     
    6        public bool isWalking = false;
    7        public bool isReoloading = false;
    8        public bool isSprinting = false;
    9     
    10        void Start() {
    11       
    12            animation.CrossFade("idle");
    13       
    14        }
    15       
    16        void Update() {
    17       
    18            if(!animation.IsPlaying("walk")) {
    19           
    20                isWalking = false;
    21           
    22            }
    23               
    24            if(Input.GetKeyDown(KeyCode.W)) {
    25           
    26                isWalking = true;
    27           
    28            }
    29           
    30            if(isWalking == true) {
    31           
    32                 animation.CrossFade("walk");
    33           
    34            }
    35            else {
    36           
    37                animation.CrossFade("idle");
    38           
    39            }
    40           
    41            if(Input.GetKeyUp(KeyCode.W)) {
    42       
    43                isWalking = false;
    44                animation.CrossFade("idle");
    45           
    46            }
    47           
    48            if(isWalking == false) {
    49           
    50                animation.CrossFade("idle");
    51           
    52            }
    53       if(Input.GetKeyUp(KeyCode.R)) {
    54       
    55                isWalking = false;
    56                animation.CrossFade(“Reload”);
    57           
    58            }
    59           
    60            if(isWalking == false) {
    61            if(isSprnting == false) {
    62           
    63                animation.CrossFade("idle");
    64           
    65            }
    66       
    67        }
    68       
    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 :).

Anyone? Would really appreciate some help here :frowning:

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)?

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.