- Home /
 
 
               Question by 
               Daniel311311 · Sep 13, 2014 at 09:34 PM · 
                c#2dmovement  
              
 
              I need to change the animation when my speed increases in a 2D game
Ok, so I have this script to make my character sprint when shift is held, only for a certain period of time. It works as far as increasing the characters movement speed. Now I need to make it so that I have a variable connected to it in the animator so I can change to my running animation while sprinting.
     `private float movementSpeed;
 //time var values
 private float coolDownTimer;
 private float boostTimer;
 //end time var values
 
 //floats in which we will store the current time left from cooldown timer and boost timer
 private float _boostTimer;
 private float _coolDownTimer;
 //temporary vars to help on the programming
 private bool done;
 private Vector3 movemement;
 private float multiplyer;
 Animator anim;
 
 // Use this for initialization
 void Start () {
     //set the duration for the boostTimer (how long the boost can last) and for coolDownTimer (how long will it take to recharge)
     boostTimer = 6.0f;
     coolDownTimer = 5.0f;
     //set movement speed here, and speedBoost value
     movementSpeed = 3;
     multiplyer = 2.5f; // <-- this right here is the times we multiply our movement speed
     
     done = false;
 }
 
 // Update is called once per frame
 void Update () {
     //we set a temp vector, and initialize it with (0, 0, 0)
     Vector3 movement;
     movement = Vector3.zero;
     
     //we check to see if input keys are pressed, then add the amount to move to the movement Vector3;
     if(Input.GetAxis("Horizontal") != 0){
         movement += transform.right * Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
     }
     if(Input.GetAxis("Vertical") != 0){
         movement += transform.forward * Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
     }
     //we can sprint coz we're moving
     if(movement != Vector3.zero){
         //we check so that we are not on cooldown and if we press shift, we initialize the boostTimer
         if(Input.GetKeyDown(KeyCode.LeftShift) && _coolDownTimer <= 0 && _boostTimer <= 0){
             _boostTimer = boostTimer;
             done = true;
         }
     }
     
     //if we keep the shift pressed and we're not on cooldown, and we still have boosTimer left
     if(Input.GetKey(KeyCode.LeftShift) && _coolDownTimer <= 0 && _boostTimer > 0){
         movement *= multiplyer;                        
     }
     
     //if the boostTimer is finished, initialize the cooldown timer
     if(_boostTimer <= 0 && done){
         _coolDownTimer = coolDownTimer;
         done = false;
     }
     
     
     //we decrease the timers
     _boostTimer -= Time.deltaTime;
     _coolDownTimer -= Time.deltaTime;
     
     
     //if timers go under 0 we set them to be 0, for fun :))
     if(_boostTimer < 0){
         _boostTimer = 0;
     }
     if(_coolDownTimer <= 0){
         _coolDownTimer = 0;
     }    
     
     
     //we apply the movement to the player
     transform.position += movement;
 }
 
 
               }`
               Comment
              
 
               
              What's the problem exactly?
Do you know how the animator works? If not, check out some tutorials. What you need is to have either a blend tree between walking and running in the animator that you send a speed variable to:
 anim.SetFloat("speed", rigidbody.velocity.magnitude")
 
                  Or two different states (walk/run) that you switch between with triggers:
 anim.SetTrigger("run");
 
                  Both are pretty standard animator things. You should check out the Animation tutorials here, and especially the scripting part here.
Your answer